开发者

Making a login screen using SharedPreferences help needed?

开发者 https://www.devze.com 2023-03-26 05:43 出处:网络
I have 2 activitys, one is where the user can update or set a new password via edittext boxes, called Password and the other activity is a login screen where they have to enter their password and then

I have 2 activitys, one is where the user can update or set a new password via edittext boxes, called Password and the other activity is a login screen where they have to enter their password and then it validates using sharedpreferences, called LogIn. The Password activity works fine. I can update or set new passwords no problem but the Login activity isn't comparing to the users inputed value. In short after they set their password up when they go to login using their password it says password is incorrect which its not. I have posted both activitys.

public class Password extends Activity implements View.OnClickListener{


public static final String PASSWORD_PREF_KEY ="passwd";

private TextView messages;
private EditText pass1;
private EditText pass2;
Button button1, button2;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.mainin);

    messages = (TextView) findViewById (R.id.text1);
    pass1 = (EditText) findViewById(R.id.password);
    pass2 = (EditText) findViewById (R.id.password_confirm);

    button1 = (Button) findViewById(R.id.ok);
    button2 = (Button) findViewById(R.id.button1);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this); }


    public void onClick(View v){
        switch(v.getId()){
        case R.id.button1:
            startActivity(new Intent(Password.this,LogIn.class));  finish(); 
        case R.id.ok:
        String p1 = pass1.getText().toString();
        String p2 = pass2.getText().toString();

        if (p1.equals(p2)) {
            if (p1.length() >=6 || p2.length() >=6) {

                SharedPreferences settings = getSharedPreferences(Password.PASSWORD_PREF_KEY,0);
                SharedPreferences.Editor editor = settings.edit();   

                editor.putBoolean(Password.PASSWORD_PREF_KEY,true);    


                editor.commit();
                messages.setText("Password updated!");
            }
            else
                messages.setText("Passwords must be at least 6 characters");
        }
        else{
            pass1.setText("");
            pass2.setText("");
            messages.setText("Passwords do not match");
            }
        }
};      

public class LogIn extends Activity {

private EditText开发者_开发问答 pass1;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.password);

    pass1 = (EditText) findViewById(R.id.et_pw);

   SharedPreferences passwd =  getSharedPreferences(Password.PASSWORD_PREF_KEY,0);
   final String p3 = passwd.getString(Password.PASSWORD_PREF_KEY,null);
    final String p1 = pass1.getText().toString(); 

    Button page1 = (Button) findViewById(R.id.btn_login); 
    page1.setOnClickListener(new View.OnClickListener() {         
        public void onClick(View view) { 
            if (p3.equals(p1)) {     
                startActivity(new Intent(LogIn.this,Main.class));  
                 } 
            else {     
                Toast.makeText(getApplicationContext(),"Incorrect Password",Toast.LENGTH_LONG).show();
                }
            };
            });
  };
    }


It looks like you are putting a boolean of true into the shared preferences instead of putting the password string. so when you compare the edit text string with the shared preference they are not going to equal each other.

0

精彩评论

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