布局文件中有一个EditText。一旦用户单击此字段,我希望它被替换为下面的元素标记。我该怎么做?
发自:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp" />
至:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<fragment
android:id="@+id/autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
发布于 2017-03-07 11:20:12
为两个视图提供id,并通过将可见性设置为GONE来最初隐藏CardView
<EditText
android:id="@+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp" />
<android.support.v7.widget.CardView
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<fragment
android:id="@+id/autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
在java类中,当用户单击“浏览”和“编辑文本”时,更改其可见性。
mEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setVisibility(View.GONE);
mCardView.setVisibility(View.VISIBLE);
}
});
发布于 2017-03-07 10:49:26
最简单的解决方案是将它们嵌入到LinearLayout或类似的东西中,并在用户单击时更改可见性。隐藏EditText并显示CardView。
发布于 2017-03-07 11:05:16
您应该将它们添加到布局中,并在默认情况下将CardView可见性设置为GONE
。
然后,在处理用户单击的类中,将EditText
可见性设置为View.GONE
,CardView
可见设置为View.VISIBLE
。
https://stackoverflow.com/questions/42655948
复制