我是 android 新手,我有一个编辑文本,我想保存通过首选项输入的文本。任何人都可以帮我....
提前致谢
在android中持久化数据实际上有3种方式:
这里有一些代码可以让你开始使用“共享首选项”
//get reference to a Shared Preferences instance
SharedPreferences preferences = context.getSharedPreferences("Some Name You Make Up", 0);
//add a string to the given Shared Preferences store by key
SharedPreferences.Editor editor = preferences.edit();
editor.putString("some_key", "some_value");
//read the value from the Shared preferences
String value = preferences.getString("some_key", " ");
这是关于共享首选项的 android 文档的链接。
http://developer.android.com/reference/android/content/SharedPreferences.html
享受!
SharedPreferences settings=getSharedPreferences("mypref", 0);
et.setText(settings.getString("tvalue"," "));
然后重写 onstop() 方法
@覆盖
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
SharedPreferences settings=getSharedPreferences("mypref", 0);
SharedPreferences.Editor editor=settings.edit();
editor.putString("tvalue", et.getText().toString());
editor.commit();
}