久久久久久久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 中的數值?)
主站蜘蛛池模板: 亚洲视频区 | 久热电影 | 99久久久久久 | 欧美一级做性受免费大片免费 | 夜夜操av | 国产成人在线播放 | 欧美精品成人一区二区三区四区 | 伊人青青久久 | 精品影院| 精品国产一区二区三区性色 | 中文亚洲视频 | 国产精品日韩高清伦字幕搜索 | 亚洲精品久久久久国产 | 毛片网站在线观看 | 成人二区三区 | 三级视频在线观看 | 国产精品呻吟久久av凹凸 | 欧美亚洲国产一区二区三区 | 国产日韩欧美中文字幕 | 国产精品久久久久久久久久 | 国产在线97 | 国产有码| www.伊人.com| 福利网站在线观看 | japanhdxxxx裸体| 成人av网站在线观看 | 精品免费国产 | 欧美性成人 | 国产区一区二区三区 | 欧美中文字幕一区 | 色婷婷九月 | 亚洲 欧美 在线 一区 | 综合久久99 | 欧美久久一区二区三区 | 99在线观看视频 | 精品免费国产视频 | 久久91 | 蜜桃在线视频 | 亚洲一区二区三区在线播放 | 每日更新av | 一级美国黄色片 |