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

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

Why EditText in a custom compound view is re-using the text entered in another compound view instance?(為什么自定義復合視圖中的 EditText 會重復使用在另一個復合視圖實例中輸入的文本?) - IT屋-程序員軟件開發
本文介紹了為什么自定義復合視圖中的 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);

}
   }

現在,如果我在我的 _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 會重復使用在另一個復合視圖實例中輸入的文本?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 538在线精品| 中文久久 | 天天艹逼网 | 久久一久久 | 日韩久久中文字幕 | 成年人视频免费在线观看 | 国产成人免费视频网站视频社区 | 99久久中文字幕三级久久日本 | 九色在线观看 | 91精品久久久久久久久 | 日日夜夜91 | 久久av网| 神马久久av | 男女啪啪高潮无遮挡免费动态 | 日韩欧美中文在线 | 久久综合九九 | 亚洲免费在线 | 亚洲国产精品suv | 成年视频在线观看福利资源 | 精品真实国产乱文在线 | 亚洲精品一级 | 亚洲高清一区二区三区 | 在线免费看黄 | 美国十次成人欧美色导视频 | 久久久婷婷 | 最新黄色毛片 | 毛片一区二区 | 日韩精品免费视频 | 亚洲一区二区三区在线播放 | 亚洲性在线 | 欧美日韩亚洲在线 | 伊人欧美视频 | 国产黄色小视频在线观看 | 一区二区三区高清在线观看 | 久久精品免费 | 日韩欧美在线观看视频网站 | 国产激情一区二区三区 | 日韩视频一区在线观看 | 亚洲精品一区av在线播放 | 国产中文字幕亚洲 | 天天天操天天天干 |