开发者

save custom values to preference in android?

I have a preferen开发者_如何学JAVAce settings for which i have configured a dialog box pop up on click of preference using the below code.

Dialog passwordDialog = new Dialog(this);
passwordDialog.setContentView(R.layout.password_dialog);
passwordDialog.setTitle("Set new password.");
passwordDialog.setCancelable(true);
passwordDialog.show();

The dialog is coming fine. However In the dialog i have a password edit text and a confirm password edit text. If these matches with each other i need to save the password to preference. I do not know how to save the value to a preference on ok click in my custom dialog. Please let me know how to do this. Thank you for your time and help.


You can retrieve the preferences, and then commit information to it :

SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
preferences.edit().putString("passwordKey", editText.getText().toString()).commit();

To add information to a preference, start by calling edit() (returns an editor), add the value you want (key/value, like a Map), and never forget to call commit() (to commit the changes).

You can then access your value using

preferences.getString("passwordKey", defaultValue);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜