【发布时间】:2013-01-19 23:15:41
【问题描述】:
我正在做一个小项目,该项目需要一个详细信息屏幕,用户可以在其中输入他的详细信息并将其永久存储。如果需要,用户还必须具有更改详细信息的选项。我查看了保存的首选项库,但它似乎没有提供这样的功能。
为了直观地了解所需的内容,类似这个屏幕应该没问题:
非常感谢任何帮助。提前致谢
【问题讨论】:
标签: java android android-layout
我正在做一个小项目,该项目需要一个详细信息屏幕,用户可以在其中输入他的详细信息并将其永久存储。如果需要,用户还必须具有更改详细信息的选项。我查看了保存的首选项库,但它似乎没有提供这样的功能。
为了直观地了解所需的内容,类似这个屏幕应该没问题:
非常感谢任何帮助。提前致谢
【问题讨论】:
标签: java android android-layout
您可以轻松地使用Shared Preferences 来存储用户的详细信息。每次打开首选项屏幕时,都可以从共享首选项中提取存储的数据并呈现给用户进行编辑。编辑完成后,新数据可以在共享首选项中更新。
还请查看this 线程以了解如何做到这一点。
【讨论】:
使用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()
}
}
【讨论】: