为什么自定义复合视图中的 EditText 会重复使用在

Why EditText in a custom compound view is re-using the text entered in another compound view instance?(为什么自定义复合视图中的 EditText 会重复使用在另一个复合视图实例中输入的文本?)

本文介绍了为什么自定义复合视图中的 EditText 会重复使用在另一个复合视图实例中输入的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写由 TextViewEditText 组成的自定义复合视图,_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 会重复使用在

基础教程推荐