我正在做一个小项目,该项目需要一个详细信息屏幕,用户可以在其中输入他的详细信息并将其永久存储。如果需要,用户还必须可以选择更改详细信息。我查看了保存的首选项库,但它似乎没有提供这样的功能。
为了直观地了解需要什么,类似这个屏幕的东西应该没问题:
任何帮助深表感谢。提前致谢
我正在做一个小项目,该项目需要一个详细信息屏幕,用户可以在其中输入他的详细信息并将其永久存储。如果需要,用户还必须可以选择更改详细信息。我查看了保存的首选项库,但它似乎没有提供这样的功能。
为了直观地了解需要什么,类似这个屏幕的东西应该没问题:
任何帮助深表感谢。提前致谢
SharedPreferences
对于您想要持久存储的这种少量数据,使用将是完美的。
// 'this' is simply your Application Context, so you can access this nearly anywhere
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
从首选项中获取:
// You can equally use KEY_LAST_NAME to get the last name, etc. They are just key/value pairs
// Note that the 2nd arg is simply the default value if there is no key/value mapping
String firstName = prefs.getString(KEY_FIRST_NAME_CONSTANT, "");
或保存:
Editor editor = prefs.edit();
// firstName being the text they entered in the EditText
editor.putString(KEY_FIRST_NAME_CONSTANT, firstName);
editor.commit();
您可以使用android中的SharedPreferences类来实现此类功能。
public void onCreate(Bundle object){
super.onCreate(object);
// Initialize UI and link xml data to java view objects
......
SharedPreferences myPref = getPreferences(MODE_PRIVATE);
nameView.setText(myPref.getString("USER_NAME", null));
passView.setText(myPref.getString("PASSWORD", null));
}
public void onStop(){
super.onStop();
if (isFinishing()) {
getPreferences(MODE_PRIVATE).edit()
.putString("USER_NAME", nameView.getText().toString())
.putString("PASSWORD", passView.getText().toString())
.commit()
}
}