問(wèn)題描述
我有一個(gè)登錄屏幕,其中包含 2 個(gè)用于用戶名和密碼的 EditText.我的要求是,在方向更改時(shí),EditText 中的輸入數(shù)據(jù)(如果有)應(yīng)保持原樣,并且還應(yīng)繪制新布局.我有 2 個(gè)布局 xml 文件 - 一個(gè)位于布局文件夾中,另一個(gè)位于在布局土地文件夾中.我正在嘗試實(shí)現(xiàn)以下兩種方法,但它們都不是完美的:
I have a Login screen which consists of 2 EditTexts for Username and Password. My requirement is that on orientation change , input data(if any) in EditText should remain as it is and a new layout should also be drawn. I have 2 layout xml files- one in layout folder and other in layout-land folder. I am trying to implement following 2 approaches but none of them is perfect:
(1) configChanges:keyboardHidden - 在這種方法中,我不在清單文件的 configChanges 中提供方向".所以我在 onCreate() 和 onConfigurationChanged() 方法中都調(diào)用了 setContentView() 方法.它滿足我的兩個(gè)要求.布局已更改,EditTexts 中的輸入數(shù)據(jù)也保持原樣.但它有一個(gè)大問(wèn)題:
(1) configChanges:keyboardHidden - In this approach, I don't provide "orientation" in configChanges in manifest file. So I call setContentView() method in both onCreate() and onConfigurationChanged() methods. It fulfills both my requirements. Layout is changed and input data in EditTexts also remains as it is. But it has a big problem :
當(dāng)用戶點(diǎn)擊登錄按鈕時(shí),ProgressDialog 會(huì)顯示,直到收到服務(wù)器響應(yīng).現(xiàn)在,如果用戶在 ProgressDialog 運(yùn)行時(shí)旋轉(zhuǎn)設(shè)備,應(yīng)用程序會(huì)崩潰.它顯示一個(gè)異常說(shuō)視圖不能附加到窗口".我嘗試使用 onSaveInstanceState 處理它(在方向更改時(shí)會(huì)調(diào)用它),但應(yīng)用程序仍然崩潰.
When user clicks on Login button, a ProgressDialog shows until server-response is received. Now if user rotates the device while ProgressDialog is running, app crashes. It shows an Exception saying "View cannot be attached to Window." I have tried to handle it using onSaveInstanceState (which DOES get called on orientation change) but app still crashes.
(2) configChanges:orientation|keyboardHidden - 在這種方法中,我在清單中提供了方向".所以現(xiàn)在我有兩種情況:
(2) configChanges:orientation|keyboardHidden - In this approach, I provide "orientation" in manifest. So now I have 2 scenarios:
(a) 如果我在 onCreate() 和 onConfigurationChanged() 中調(diào)用 setContentView() 方法,布局會(huì)相應(yīng)更改,但 EditText 數(shù)據(jù)會(huì)丟失.
(a) If I call setContentView() method in both onCreate() and onConfigurationChanged(), Layout is changed accordingly but EditText data is lost.
(b) 如果我在 onCreate() 中調(diào)用 setContentView() 方法,但在 onConfigurationChanged() 中沒(méi)有調(diào)用,則 EditText 數(shù)據(jù)不會(huì)丟失,但布局也不會(huì)相應(yīng)更改.
(b) If I call setContentView() method in onCreate() , but not in onConfigurationChanged(), then EditText data is not lost but layout also not changes accordingly.
在這種方法中,甚至不會(huì)調(diào)用 onSaveInstanceState().
And in this approach, onSaveInstanceState() is not even called.
所以我處于一個(gè)非常可怕的境地.這個(gè)問(wèn)題有什么解決辦法嗎?請(qǐng)幫忙.提前謝謝.
So I am in a really intimidating situation. Is there any solution to this problem? Please help. Thanx in advance.
推薦答案
在onConfigurationChanged方法中,先在全局變量中獲取兩個(gè)編輯文本的數(shù)據(jù),然后調(diào)用setContentView方法.現(xiàn)在將保存的數(shù)據(jù)再次設(shè)置到編輯文本中.
in onConfigurationChanged method, first get the data of both the edit texts in global variables and then call setContentView method. Now set the saved data again into the edit texts.
這篇關(guān)于如何在方向更改時(shí)保留 EditText 數(shù)據(jù)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!