0

我正在制作一个包含表单的应用程序,每当数据单击一个按钮时,就会从 EditText 中的 BD 加载,但每次我按下不同的按钮时,其他 EditText 都会被清除,我尝试使用:

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("data", myVariable);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
   super.onRestoreInstanceState(savedInstanceState);
   other = savedInstanceState.getString("data");
   name.setText(other);

}

抱歉,如果我没有解释清楚,每次他们更改 Activity 变量时我都需要它并且我没有删除。有什么建议么?谢谢!

4

1 回答 1

0

尝试改用安卓SharedPreferences。这是 Android 为其应用程序提供的持久键值存储,旨在解决此类问题。这是使用它的简单方法:

SharedPreference prefs = PreferenceManager.getDefaultSharedPreferences(this);

// To put a data (in this case a String from your EditText)..
Editor editor = prefs.edit();
editor.putString("data", yourStringHere);
editor.commit();

...

// ..and to retrieve it..
prefs.getString("data", null);

// NOTE: The 'null' in above method call could be replaced by any String you want;
// it basically specifies a default value to get when "data" is empty or doesn't 
// yet exist.

希望这可以帮助。

于 2014-11-24T02:23:30.963 回答