Android - Convert Button to TextView at runtime
Is it possible to convert a Button in开发者_运维问答to a TextView onclick during runtime?
Thanks Chris
What you are trying to do is a bad design.
Hide the button and show instead a TextView.
You can define a Layout that hold both TextView and Button.
At runtime just set ones setVisibility(Visibility.VISIBLE)
and the other setVisibility(Visibility.INVISIBLE)
you can use ViewSwitcher that is very best technique Here just change edittext with button Simple :)
e.g
XML
<ViewSwitcher
android:id="@+id/my_switcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/clickable_text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="TextViewClicked"
android:text="abc"
android:textColor="@color/white"
android:textSize="16sp" >
</TextView>
<EditText
android:id="@+id/hidden_edit_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="12"
android:text="12"
android:textColor="@color/white"
android:textSize="16sp" >
</EditText>
</ViewSwitcher>
JAVA
ViewSwitcher my_switcher;
my_switcher = (ViewSwitcher) findViewById(R.id.my_switcher);
TextView profile_name = (TextView) findViewById(R.id.clickable_text_view);
profile_name.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
TextViewClicked();
}
});
TextViewClicked()
public void TextViewClicked() {
my_switcher.showNext(); //or switcher.showPrevious();
TextView myTV = (TextView) my_switcher.findViewById(R.id.clickable_text_view);
myTV.setText("value");
}
Hope it help
精彩评论