0

我想在我的应用程序中实现密码更改活动,这是我为我的活动编写的代码,但我认为我应该将字符串密码变量声明到其他地方,因为我的新密码成功更改并且可以工作直到我关闭了应用程序,当我再次运行它时,旧密码是正确的。我对 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();
        }

    }
4

2 回答 2

1

这里

if(pass.equalsIgnoreCase(Change_Password.getPassword()))

您正在尝试从以前的 Activity 调用一个方法,这不是在 Activity 或 Application 的其他组件之间共享数据的有效方式。

在您的情况下,您可以使用SharedPreferences来存储密码,而不是使用static字段或方法

请参阅我们在应用程序中使用 SharedPreferences 的这些教程:

http://developer.android.com/guide/topics/data/data-storage.html

http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html

http://android-er.blogspot.in/2011/01/example-of-using-sharedpreferencesedito.html

于 2012-12-26T08:57:39.330 回答
1

这里的问题是您的所有内容都在内存中运行,我的意思是,您的密码变量(静态)在内存中,您在更改密码时分配了它并且没问题。

但是当你再次启动应用程序时,密码的值为1234,因为你有密码!xD,您必须将密码存储在其他地方,例如使用 SharedPreferences。

于 2012-12-26T09:03:24.380 回答