問題描述
我正在嘗試編寫由 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);
}
}
現(xiàn)在,如果我在我的 _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
中寫一些東西并旋轉(zhuǎn)我的設備時,相同的 text
出現(xiàn)在第一個自定義 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
.
為什么會發(fā)生這種行為?
Why is happening this behaviour?
我通過刪除 android:id 并為 Compound_view.xml 中的 EditText
使用 android:tag 解決了這個問題,然后管理保存CompoundView 類中的 EditText 狀態(tài):
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 操作系統(tǒng)具有保存和恢復視圖狀態(tài)的默認功能.
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.現(xiàn)在,當 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.
這篇關(guān)于為什么自定義復合視圖中的 EditText 會重復使用在另一個復合視圖實例中輸入的文本?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!