2015-11-30 82 views
0

按照這個順序,這是我想要實現的過程。存儲在SharedPreferences中的密碼不能在android中使用?

1)當按下imageButton時,它檢查存儲在SharedPreferences中的「pass」的值。如果這與密碼匹配,意圖開始下一個活動。 2)如果它爲空或不匹配密碼,會彈出一個對話框,提示輸入用戶名和密碼。

3)如果輸入的密碼正確,它將用戶名和密碼寫入SharedPreferences。

4)如果輸入的密碼不正確,則會發出Toast指示。

到目前爲止,登錄工作正常。但是,我似乎無法使SharedPreferences函數正常工作。我的代碼如下:

Button aliaLogin = (Button)findViewById(R.id.imageButton); 
    aliaLogin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      SharedPreferences prefs = getSharedPreferences("testapp", MODE_PRIVATE); 
      SharedPreferences.Editor editor = prefs.edit(); 
      editor.putString("username","7"); 
      editor.putString("pass","11"); 
      editor.commit(); 
      String username = prefs.getString("username",null); 
      String pass = prefs.getString("pass",null); 
      if (pass != null && !pass.isEmpty() && pass.equals(housePass[5])){ 
       Intent intent = new Intent(getApplicationContext(), DisplayHouse.class); 
       intent.putExtra("Username", username); 
       startActivity(intent); 
      } 
      else { 

       final Dialog dialog = new Dialog(HouseMain.this); 
       dialog.setTitle("Login Required!"); 
       dialog.setContentView(R.layout.login_toast); 
       dialog.show(); 

       final EditText name = (EditText) dialog.findViewById(R.id.name); 
       final EditText password = (EditText) dialog.findViewById(R.id.password); 
       Button submit = (Button) dialog.findViewById(R.id.submitButton); 
       Button cancel = (Button) dialog.findViewById(R.id.cancelButton); 

       submit.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 

         String username = name.getText().toString(); 
         String pass = password.getText().toString(); 
         SharedPreferences prefs = getSharedPreferences("testapp", MODE_PRIVATE); 
         SharedPreferences.Editor editor = prefs.edit(); 

         if (pass.equals(housePass[5])) { 
          Intent intent = new Intent(getApplicationContext(), DisplayHouse.class); 
          intent.putExtra("Username", username); 
          editor.putString("username", username); 
          editor.putString("pass", pass); 
          editor.commit(); 
          startActivity(intent); 
         } else { 
          Toast.makeText(getApplicationContext(), "Incorrect! Impostor alert!", Toast.LENGTH_SHORT).show(); 
         } 

         dialog.cancel(); 

        } 

       }); 
       cancel.setOnClickListener(new View.OnClickListener(){ 
        @Override 
        public void onClick(View v) { 
         dialog.cancel(); 
        } 

       }); 

      } 


     } 
    }); 

有人能告訴我我做錯了什麼嗎?

+0

這是什麼'housePass [5]'? –

+1

爲什麼每次單擊按鈕時都要設置首選項? – droidev

+0

@RaviRupareliya這是正確的密碼。 –

回答

2

的問題是,每次當你要重設用戶名和密碼,單擊按鈕爲默認

SharedPreferences prefs = getSharedPreferences("testapp", MODE_PRIVATE); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString("username","7"); 
     editor.putString("pass","11"); 
     editor.commit(); 

你必須從你的代碼中刪除它

+0

對不起,沒有看到這個或我肯定會接受!非常感謝! –

0

你必須覆蓋通保存到其他通首選項文件:

SharedPreferences prefs = getSharedPreferences("testapp", MODE_PRIVATE); 
SharedPreferences.Editor editor = prefs.edit(); 
editor.putString("username","7"); 
editor.putString("pass","11"); 
editor.commit(); 
相關問題