我想在我的应用程序中实现密码更改活动,这是我为我的活动编写的代码,但我认为我应该将字符串密码变量声明到其他地方,因为我的新密码成功更改并且可以工作直到我关闭了应用程序,当我再次运行它时,旧密码是正确的。我对 Android 开发真的很陌生,任何答案/建议都将不胜感激。
Change_Password 代码:
public class Change_Password extends Activity {
//----
public SharedPreferences prefs;
private String prefName = "MyPref";
private static final String TEXT_VALUE_KEY = "nothing";
//-----
public static String Password="soha";
public static String getPassword()
{
return Password;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.change_password);
Button btnCancel=(Button)findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
final EditText txtNewPassword=(EditText)findViewById(R.id.txtNewPassword);
final EditText txtCurrentPassword=(EditText)findViewById(R.id.txtCurrentPassword);
final EditText txtConfirmPassword=(EditText)findViewById(R.id.txtConfirmNewPassword);
Button btnSave=(Button)findViewById(R.id.btnSave);
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(txtConfirmPassword.getText().toString().equalsIgnoreCase("") |
txtCurrentPassword.getText().toString().equalsIgnoreCase("") |
txtCurrentPassword.getText().toString().equalsIgnoreCase(""))
{
Toast.makeText(getBaseContext(),"Please Complete the Information", Toast.LENGTH_SHORT).show();
}
else
if(!txtNewPassword.getText().toString().equalsIgnoreCase(txtConfirmPassword.getText().toString()))
{
Toast.makeText(getBaseContext(),
"These Passwords Don't Match !", Toast.LENGTH_SHORT).show();
}
else
if(!getPassword().equalsIgnoreCase(txtCurrentPassword.getText().toString()))
{
Toast.makeText(getBaseContext(),
"Current Password is Incorrect!", Toast.LENGTH_SHORT).show();
}
else
{
///------- //---save the values in the EditText view to preferences---
prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(TEXT_VALUE_KEY, txtNewPassword.getText().toString());
//---saves the values---
editor.commit();
///--------
//Password=txtNewPassword.getText().toString();
Toast.makeText(getBaseContext(),
Password, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
BackToMainIntent();
else if(keyCode==KeyEvent.KEYCODE_BACK)
{
BackToMainIntent();
}
return false;
}
public void BackToMainIntent()
{
Intent intent = new Intent(this, Main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
这是我的主要活动:
public class mainC extends Activity {
private EditText uPass;
private Button loginBtn;
private Button Btn_Exit;
private ImageView Image;
///------
public SharedPreferences prefs;
private String prefName = "MyPref";
private static final String TEXT_VALUE_KEY = "1234";
////----------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
Image=(ImageView)findViewById(R.id.NFCImage);
Image.setAlpha(100);
Btn_Exit=(Button)this.findViewById(R.id.Btn_exit_app);
Btn_Exit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent _Intent =new Intent(Intent.ACTION_MAIN);
_Intent.addCategory(Intent.CATEGORY_HOME);
_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(_Intent);
}
});
setUpViews();
}
@Override
protected void onResume() {
super.onResume();
uPass.setText("");
uPass.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);
uPass.setTextColor(Color.parseColor("#888888"));
}
private void setUpViews() {
uPass=(EditText)findViewById(R.id.usrPassTxt);
uPass.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
uPass.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);
uPass.setText("");
}
}
});
loginBtn=(Button)findViewById(R.id.Btn_Login);
loginBtn.setOnClickListener(new OnClickListener() {
private String pass;
Intent myIntent;
public void onClick(View v) {
pass=uPass.getText().toString();
///-----
SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
String passtemp = prefs.getString(TEXT_VALUE_KEY, "nothing");
if( pass.equalsIgnoreCase(passtemp))
///-----
//if( pass.equalsIgnoreCase(Change_Password.getPassword()))
{
myIntent=new Intent(mainC.this,Main.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
System.out.println("---IF---");
}
else{
myIntent=new Intent(mainC.this,ErrorPage.class);
System.out.println("---ELSE---");
}
startActivity(myIntent);
}
});
}
@Override
public void onBackPressed() {
finish();
}
}