**当应用程序打开非物质性时会询问密码。最初的密码是静态的(1111)。在主页活动中输入用户时,更改密码选项是否可用。请您帮忙完成此操作...谢谢
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;
答案 0 :(得分:2)
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)
答案 1 :(得分:1)
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);