如何留住的方向变化的EditText数据?方向、数据、EditText

2023-09-03 23:03:32 作者:翩翩喜欢祢ゝ

我有它由2 EditTexts用户名和密码登录屏幕。 我的要求是,在方向改变,输入数据(如果有的话)的EditText应该保持,因为它是一个新的布局也应绘制。我有2个布局XML布局文件和其他文件 - 1在布局土地文件夹。我想实现以下两个办法,但他们都不是完美的:

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取向研究。所以,我呼吁的setContentView()在这两个的onCreate()和onConfigurationChanged()方法的方法。它可以同时满足我的要求。布局被改变并且在EditTexts输入数据也保持原样。但它有一个很大的问题:

(1) configChanges:keyboardHidden - In this approach, I don't provide "orientaion" 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 :

当用户点击登录按钮,一个 ProgressDialog显示,直到收到服务器的响应。现在,如果用户旋转设备时ProgressDialog运行时,应用程序崩溃。它显示了一个异常说视图不能连接到窗口。我曾尝试使用的onSaveInstanceState(它被调用的方向变化),但是应用程序仍然崩溃来处理它。

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:定位| keyboardHidden - 在这种方法中,我的表现提供定向。所以,现在我有2个方案:

(2) configChanges:orientation|keyboardHidden - In this approach, I provide "orientation" in manifest. So now I have 2 scenarios:

(一)如果我打电话的setContentView()在这两个的onCreate()和onConfigurationChanged()方法,布局也相应改变,但EditText上的数据都会丢失。

(a) If I call setContentView() method in both onCreate() and onConfigurationChanged(), Layout is changed accordingly but EditText data is lost.

(二)如果我打电话的setContentView()方法中的onCreate(),但不是在onConfigurationChanged(),那么EditText上的数据不会丢失,但布局上也没有发生相应变化。

(b) If I call setContentView() method in onCreate() , but not in onConfigurationChanged(), then EditText data is not lost but layout also not changes accordingly.

而在这种方法中,的onSaveInstanceState()甚至不叫。

And in this approach, onSaveInstanceState() is not even called.

所以,我在一个很吓人的局面。有没有办法解决这个问题?请帮忙。感谢名单提前。

So I am in a really intimidating situation. Is there any solution to this problem? Please help. Thanx in advance.

推荐答案

在onConfigurationChanged方法,首先获得无论是在全局变量编辑文本的数据,然后调用的setContentView方法。现在再次设置保存数据到编辑文本。

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.