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

EditText 不會在屏幕方向更改時自動保存

EditText not automatically saved on screen orientation change(EditText 不會在屏幕方向更改時自動保存)
本文介紹了EditText 不會在屏幕方向更改時自動保存的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我了解到,當應用程序即將停止或終止時,Android 會自動保存 EditText 對象的內容.但是,在我的應用中,當屏幕方向改變時,EditText 的內容會丟失.

I read that Android automatically saves the content of EditText objects when an application is about to be stopped or killed. However, in my app the content of an EditText is lost when screen orientation changes.

這是正常行為嗎?然后我是否必須使用 onSaveInstanceState/onRestoreInstanceState 手動保存/恢復其內容?或者有沒有更簡單的方法告訴Android保存它并恢復它?

Is it normal behaviour? Do I then have to manually save/restore its content with onSaveInstanceState/onRestoreInstanceState? Or is there an easier method to tell Android to save it end restore it?

編輯:

我以編程方式創建 EditText 對象,而不是在 XML 中.事實證明這與問題有關(請參閱下面接受的答案).

I create the EditText object programmatically, not in XML. This turns out to be related to the problem (see accepted answer below).

推薦答案

這不是正常行為.

首先,確保您在布局 XML 中為 EditText 控件分配了 ID.

First and foremost, ensure that you have IDs assigned to your EditText controls in the layout XML.

它只需要一個ID,句號.如果您以編程方式執行此操作,除非它有 ID,否則它將丟失狀態.

Edit 1: It just needs an ID, period. If you're doing this programmatically, it will lose state unless it has an ID.

因此,將其用作快速 &骯臟的例子:

So using this as a quick & dirty example:

    // Find my layout
    LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.ll1);
    // Add a new EditText with default text of "test"
    EditText testText = new EditText(this.getApplicationContext());
    testText.setText("test");


    // This line is the key; without it, any additional text changes will 
    // be lost on rotation. Try it with and without the setId, text will revert
    // to just "test" when you rotate.

    testText.setId(100); 

    // Add your new EditText to the view.
    mLinearLayout.addView(testText);

這會解決你的問題.

如果失敗,您需要自己保存和恢復狀態.

Should that fail, you'll need to save and restore state yourself.

像這樣覆蓋 onSaveInstanceState:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("textKey", mEditText.getText().toString());
}

然后在OnCreate中恢復:

public void onCreate(Bundle savedInstanceState) {
    if(savedInstanceState != null)
    {
        mEditText.setText(savedInstanceState.getString("textKey"));
    }
}

另外,請不要使用 android:configChanges="orientation" 來嘗試完成此操作,這是錯誤的方法.

Also, please don't use android:configChanges="orientation" to try to accomplish this, it's the wrong way to go.

這篇關于EditText 不會在屏幕方向更改時自動保存的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出)
Multiline EditText with Done SoftInput Action Label on 2.3(2.3 上帶有完成 SoftInput 操作標簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測向左或向右滑動?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設置為僅接受 Android 中的數值?)
主站蜘蛛池模板: 国产成人一区二区三区 | 午夜久久久久久久久久一区二区 | 欧美一区二区三区在线观看 | 日韩av最新网址 | 精品一区二区在线观看 | 黄色一级大片在线免费看产 | 亚洲国产成人av好男人在线观看 | 91精品www | 91在线看 | 四虎影音 | 国产美女一区二区 | 祝你幸福电影在线观看 | 欧美日韩综合一区 | 国产一二三区精品视频 | 美女黄频 | 日韩欧美天堂 | 黄 色 毛片免费 | 国产精品视频www | 国产精品久久久一区二区三区 | 亚洲成人第一页 | 99亚洲精品 | 精品一二三 | av av在线 | 亚洲精品国产综合区久久久久久久 | 国产免费福利小视频 | 成人精品在线观看 | 99视频精品 | 日韩在线国产 | 亚洲精品免费在线 | 免费激情av | 久久久久久久久久一区 | 久久久久国产 | 97人人爱 | 国产精品美女久久久久久免费 | 精品综合| 欧美一级做性受免费大片免费 | 国产在线中文字幕 | 亚洲一区二区三区在线 | 福利视频网址 | 亚洲一区中文字幕在线观看 | 免费a级毛片在线播放 |