如何在方向更改时保留 EditText 数据?

时间:2023-05-04
本文介绍了如何在方向更改时保留 EditText 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个登录屏幕,其中包含 2 个用于用户名和密码的 EditText.我的要求是,在方向更改时,EditText 中的输入数据(如果有)应保持原样,并且还应绘制新布局.我有 2 个布局 xml 文件 - 一个位于布局文件夹中,另一个位于在布局土地文件夹中.我正在尝试实现以下两种方法,但它们都不是完美的:

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() 方法中都调用了 setContentView() 方法.它满足我的两个要求.布局已更改,EditTexts 中的输入数据也保持原样.但它有一个大问题:

(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 :

当用户点击登录按钮时,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:orientation|keyboardHidden - 在这种方法中,我在清单中提供了方向".所以现在我有两种情况:

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

(a) 如果我在 onCreate() 和 onConfigurationChanged() 中调用 setContentView() 方法,布局会相应更改,但 EditText 数据会丢失.

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

(b) 如果我在 onCreate() 中调用 setContentView() 方法,但在 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.

这篇关于如何在方向更改时保留 EditText 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:SoftKeyboard 隐藏 EditText 下一篇:如何以编程方式在 Android 中设置可编辑的真/假

相关文章

最新文章