2011-08-03 2 views
1

2 가지 활동이 있습니다. 하나는 사용자가 암호라는 편집 상자를 통해 새 암호를 업데이트하거나 설정할 수있는 곳이며 다른 활동은 암호를 입력해야하는 로그인 화면이며 LogIn이라는 sharedpreferences를 사용하여 유효성을 검사합니다. 암호 활동이 정상적으로 작동합니다. 새 비밀번호를 업데이트하거나 설정할 수 있지만 로그인 활동은 사용자가 입력 한 값과 비교하지 않습니다. 잠시 후에 그들은 암호를 사용하여 로그인 할 때 암호를 설정 한 후 암호가 틀렸다고 말합니다. 나는 두 가지 활동을 올렸다.SharedPreferences를 사용하여 로그인 화면을 만드는 데 도움이 필요합니까?

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(); 
       } 
      }; 
      }); 
    }; 
    } 

답변

0

암호 문자열을 입력하는 대신 공유 환경 설정에 true 값을 넣는 것처럼 보입니다. 따라서 편집 텍스트 문자열을 공유 환경 설정과 비교할 때 서로가 같지 않을 것입니다.

+0

당신은 editor.putBoolean (Password.PREF_KEY, true)에 대해 이야기하고 있습니까? 내가 무엇을 바꿀 필요가 있니? – Jonathan

+0

당신의 논리는 바로 지금 그들이 새로운 암호를 입력 할 때 환경 설정에 진실하다는 것입니다. 암호가 맞는지 확인해야한다면 암호가 일치하는지 확인해야합니다. 그래서 대신 editor.putString (Password.PASSWORD_PREF_KEY, p1); 이렇게하면 암호를 문자열로 저장합니다. – Pengume

+0

editor.putString (Password.PASSWORD_PREF_KEY, p1);으로 변경했습니다. u와 같은 제안하지만 난 여전히 잘못된 암호가 :/ – Jonathan

관련 문제