如何在 android 中更新共享首选项字符串(密码)
How to update shared preference string (password) in android
**当应用程序打开时,非物质性会询问 password.Initially 密码是静态的,如 (1111)。当在主页 activity 中输入用户时,更改密码选项是 available.Could 请帮助如何执行此操作...谢谢
1. MainActivity.java
String storedPassword = "1111";
strPassword=tv_password.getText().toString();
if(storedPassword.equals(strPassword)) {
Intent MainIntent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(MainIntent);
}else {
Toast.makeText(MainActivity.this,"Please check your password",Toast.LENGTH_LONG).show();
}
2. Homeactivity.java
public class HomeActivity extends Activity implements View.OnClickListener {
private Button bt_change_password;
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Edit Password");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 30, 0);
EditText first_password = new EditText(HomeActivity.this);
EditText secound_password = new EditText(HomeActivity.this);
first_password.setHint("new password");
secound_password.setHint("new password(again)");
layout.addView(first_password, params);
layout.addView(secound_password, params);
alert.setView(layout);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do nothing
}
});
alert.show();
break;
public class PrefManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "MY_TAG";
private static final String PASS_TAG= "my_pass_tag";
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void Setpassword(String password) {
editor.putString(PASS_TAG, password);
editor.commit();
}
public int getpassword() {
return pref.getString(PASS_TAG);
}}
然后就可以使用了
PrefManager prefManager;
prefManager = new PrefManager(this);
String _act = prefManager.getpassword();
prefManager.setpassword(password)
Problem is resolved...
set Shared Pref:
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
userId= first_password.getText().toString();
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(HomeActivity.this);
SharedPreferences.Editor edit = settings.edit();
edit.putString("name", userId);
edit.commit();
}
get shared pref:
SharedPreferences settins = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
strPassword=settins.getString("name", storedPassword);
**当应用程序打开时,非物质性会询问 password.Initially 密码是静态的,如 (1111)。当在主页 activity 中输入用户时,更改密码选项是 available.Could 请帮助如何执行此操作...谢谢
1. MainActivity.java
String storedPassword = "1111";
strPassword=tv_password.getText().toString();
if(storedPassword.equals(strPassword)) {
Intent MainIntent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(MainIntent);
}else {
Toast.makeText(MainActivity.this,"Please check your password",Toast.LENGTH_LONG).show();
}
2. Homeactivity.java
public class HomeActivity extends Activity implements View.OnClickListener {
private Button bt_change_password;
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Edit Password");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 30, 0);
EditText first_password = new EditText(HomeActivity.this);
EditText secound_password = new EditText(HomeActivity.this);
first_password.setHint("new password");
secound_password.setHint("new password(again)");
layout.addView(first_password, params);
layout.addView(secound_password, params);
alert.setView(layout);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do nothing
}
});
alert.show();
break;
public class PrefManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "MY_TAG";
private static final String PASS_TAG= "my_pass_tag";
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void Setpassword(String password) {
editor.putString(PASS_TAG, password);
editor.commit();
}
public int getpassword() {
return pref.getString(PASS_TAG);
}}
然后就可以使用了
PrefManager prefManager;
prefManager = new PrefManager(this);
String _act = prefManager.getpassword();
prefManager.setpassword(password)
Problem is resolved...
set Shared Pref:
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
userId= first_password.getText().toString();
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(HomeActivity.this);
SharedPreferences.Editor edit = settings.edit();
edit.putString("name", userId);
edit.commit();
}
get shared pref:
SharedPreferences settins = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
strPassword=settins.getString("name", storedPassword);