检查首选项是否返回“默认值”,如果是,则显示一个对话框并将这些值提交给首选项。
Editor editor = prefs.edit();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String p = prefs.getString("prefUsername", "default");
if (p.equals("default")) {
createDialogBox();
p = prefs.getString("prefUsername", "default");
}
public void createDialogBox()
{
final EditText et = new EditText(this);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set title
alertDialogBuilder.setTitle("Please give Input");
// set dialog message
alertDialogBuilder
.setMessage("Input")
.setView(et)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String c = et.getText().toString().trim();
editor.putString("prefsUsername", c);
editor.commit();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}