EditText上的数据丢失旋转设备 [英] EditText data is lost on rotating the device
问题描述
确定家伙,我用它做。我一直在使用Google的最后2天,但我无法找到确切的解决方案。每个人都在谈论configChanges以及所有那些陈词滥调事情,似乎并不在我的情况下工作。
OK guys, I am done with it. I have been googling for last 2 days but I can't find the exact solution. Everyone is talking about configChanges and all those cliche things which do not seem to work in my case .
我有一个登录界面,下设2 EditTexts。现在,这个登录界面有两个纵向和横向不同的布局。所以我不得不创建布局文件夹使用不同login.xml和布局,土地文件夹中其他login.xml。并支持方向改变我overrid在LoginActivity类onConfigurationChanged()方法。在这种方法我称之为的setContentView(R.layout.login)方法,以便适当login.xml每天来回方向设置布局。
I have a login screen which consists of 2 EditTexts. Now this login screen has different layout for both Portrait and Landscape orientation . So I had to create one login.xml in layout folder and another login.xml in layout-land folder. And to support orientation changes I overrid onConfigurationChanged() method in LoginActivity class. In this method I call setContentView(R.layout.login) method so that appropriate login.xml is set as layout fro every orientation.
这一切后,我定义我的清单文件下面还有:
After all this I defined following in my manifest file as well:
android:configChanges="orientation|keyboardHidden"
不过还是我对着著名的老问题。如果在该EDITTEXT任何文本和装置被旋转时,该文本将丢失。我不想失去的文本。是否有可能或不?我已阅读,我们可以使用的onSaveInstanceState(捆绑savedInstanceState)方法做到这一点,我甚至已尝试过,但它din't为我工作。请帮忙。先谢谢了。
But still I am facing the famous old problem. If there is any text in the Edittext and the device is rotated, that text is lost. I don't want to lose that text. Is it possible or not? I have read that we can do it using onSaveInstanceState(Bundle savedInstanceState) method and I have even tried that too but it din't work for me. Please help. Thanks in advance.
推荐答案
使用看跌期权的方法来储存的onSaveInstanceState中值:
Use the put methods to store values in onSaveInstanceState:
protected void onSaveInstanceState(Bundle extra) {
super.onSaveInstanceState(extra);
extra.putString("text", "your text here");
}
和中的onCreate恢复值:
And restore the values in onCreate:
public void onCreate(Bundle extra) {
if (extra != null) {
String value = extra.getString("text");
}
}
编辑(什么实际工作):
EDIT(What actually worked):
尝试删除Android:configChanges = |从清单方向keyboardHidden
try to delete android:configChanges="orientation|keyboardHidden" from manifest.
祝你好运!
这篇关于EditText上的数据丢失旋转设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!