你的XML應該是這樣的,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
現在,下面的代碼添加到您的活動類。
//init the views.
et1.setVisiblity(GONE);
tv1.setVisiblity(VISIBLE);
//if the textView is shown, the btn click will hide it and show editText
//instead, and same happens for editText
btn1.setOnClickListener(new View.OnClickListener(){
public void onClick(){
if(et1.getVisiblity() != VISIBLE) {
et1.setVisbility(VISIBLE);
tv1.setVisibility(GONE);
} else {
et1.setVisbility(GONE);
tv1.setVisibility(VISIBLE);
}
} });
P.S:這不是可執行代碼,儘管希望它能讓你明白。
只要在'Edittext'中刪除這些行,就可以使用android:background =「null」'並且使遊標不可見,'android:cursorVisible =「false」'。然後使用該按鈕單擊,將背景設置爲「EditText」並使光標再次可見。 –
歡迎來到StackOverflow,@Fatima Saif!我們很樂意爲您提供幫助,但您的問題並非[最小,可驗證和完整](https://stackoverflow.com/help/mcve)。請考慮加入你的問題,以便我們能夠幫助你。 – AJFarmar
同時使用它們,'editText'應該是默認不可見的,點擊後它將變得可見,但'textView'將不可見。 – samirk433