久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么自定義復合視圖中的 EditText 會重復使用在

Why EditText in a custom compound view is re-using the text entered in another compound view instance?(為什么自定義復合視圖中的 EditText 會重復使用在另一個復合視圖實例中輸入的文本?) - IT屋-程序員軟件開發(fā)
本文介紹了為什么自定義復合視圖中的 EditText 會重復使用在另一個復合視圖實例中輸入的文本?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試編寫由 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);

}
   }

現(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 精品国产一区探花在线观看 | 日韩欧美一级精品久久 | 欧美激情黄色 | 久久久久久久久久久高潮一区二区 | 久操国产| 国产精品视频一区二区三区不卡 | 国产一区二区欧美 | 午夜欧美一区二区三区在线播放 | 欧美精品欧美精品系列 | 午夜影晥 | 成人免费观看视频 | 91精品国产色综合久久 | 久草热8精品视频在线观看 午夜伦4480yy私人影院 | 色播av| 日日想夜夜操 | 久久av一区二区三区 | 国产精品综合久久 | 亚洲视频一 | 99精品一区二区 | 久久精品视频网站 | 国产免费福利 | av黄色免费 | 午夜精品一区二区三区在线视 | 欧美一区二区三区视频在线播放 | 国产一区二区三区四区在线观看 | 久久久久国产精品一区二区 | 午夜精品一区二区三区在线观看 | 9999久久 | 一区二区三区国产精品 | 国产目拍亚洲精品99久久精品 | 成人欧美一区二区三区在线观看 | 一级做a爰片性色毛片16美国 | 国产中文字幕在线 | 久久久123 | 欧美日韩a | 亚洲精品一 | 欧美激情综合 | 国产成人免费在线 | 一本色道久久综合亚洲精品高清 | 日韩中文一区二区三区 | 精品蜜桃一区二区三区 |