Why EditText in a custom compound view is re-using the text entered in another compound view instance?(为什么自定义复合视图中的 EditText 会重复使用在另一个复合视图实例中输入的文本?)
问题描述
我正在尝试编写由 TextView
和 EditText
组成的自定义复合视图,_compound_view.xml_:
I'm trying to write a custom compound view composed by a TextView
and an EditText
, _compound_view.xml_:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/compoundText"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label" />
<EditText
android:id="@+id/textEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter text here" >
</EditText>
这是扩展LinearLayout
的类:
public class CompoundView extends LinearLayout {
public CompoundView(Context context, AttributeSet attrs) {
super(context, attrs);
readAttributes(context, attrs);
init(context);
}
public CompoundView(Context context) {
super(context);
init(context);
}
private void init(Context c) {
final LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.compound_view, this);
}
}
现在,如果我在我的 _activity_main.xml_ 中使用其中的 2 个 View
:
Now, if I use 2 of these View
in my _activity_main.xml_:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<it.moondroid.compoundview.example.CompoundView
android:id="@+id/compoundview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<it.moondroid.compoundview.example.CompoundView
android:id="@+id/compoundview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/compoundview1" />
</RelativeLayout>
在 Activity 代码中,我只对 RelativeLayout 进行膨胀,而不管理 onSaveInstanceState:
and in the Activity code I only inflate the RelativeLayout, without managing onSaveInstanceState:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
然后当我在第二个 EditText
中写一些东西并旋转我的设备时,相同的 text
出现在第一个自定义 View
.
then when I write something in the 2nd EditText
and I rotate my device, the same text
appears in the EditText of the first custom View
.
为什么会发生这种行为?
Why is happening this behaviour?
我通过删除 android:id 并为 Compound_view.xml 中的 EditText
使用 android:tag 解决了这个问题,然后管理保存CompoundView 类中的 EditText 状态:
I solved the issue by removing android:id and using android:tag for the EditText
in compound_view.xml, then managing the saving of the EditText state in CompoundView class:
@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable("instanceState", super.onSaveInstanceState());
bundle.putString("currentEdit", mEditText.getText().toString());
bundle.putBoolean("isFocused", mEditText.hasFocus());
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
mEditText.setText(bundle.getString("currentEdit"));
if (bundle.getBoolean("isFocused")) {
mEditText.requestFocus();
}
super.onRestoreInstanceState(bundle.getParcelable("instanceState"));
return;
}
super.onRestoreInstanceState(state);
}
推荐答案
你需要禁用 SaveEnabled 属性使用 android:saveEnabled="false"
You need to disable SaveEnabled property of EditText using android:saveEnabled="false"
在您的自定义视图中,您正在从定义了 ID 的 XML 扩展布局.如果视图定义了 ID,Android 操作系统具有保存和恢复视图状态的默认功能.
In your custom view, you are inflating layout from XML which has ID defined. Android OS has default functionality to save and restore the state of the view if the view has ID defined.
表示当Activity暂停时会保存EditText的文本,当Activity恢复时会自动恢复.在您的情况下,您多次使用此自定义视图,并且正在膨胀相同的布局,因此您的所有 EditText 都具有相同的 ID.现在,当 Activity 暂停时,Android 将检索 EditText 的值并保存它们的 ID,但由于每个 EditText 具有相同的 ID,值将被覆盖,因此它将在所有 EditText 中恢复相同的值.
It means that it will save the text of the EditText when Activity gets paused and restore automatically when Activity gets restored. In your case, you are using this custom view multiple times and that is inflating the same layout so your all EditText have the same ID. Now when Activity will get pause Android will retrieve the value of the EditText and will save against their ID but as you have the same ID for each EditText, values will get override and so it will restore same value in all your EditText.
这篇关于为什么自定义复合视图中的 EditText 会重复使用在另一个复合视图实例中输入的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么自定义复合视图中的 EditText 会重复使用在
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01