2016-05-22 24 views
0

我正在爲兩種類型的用戶開發一個應用程序(我的畢業項目),在登錄頁面有一個複選框記住用戶的憑據,但是當它被選中的憑據顯示在兩個登錄頁面 我使用SharedPreferences我有兩種類型的用戶登錄頁面,只要我點擊記住我的字段保存在兩個登錄頁面

從登錄

的代碼片段,餐廳:

public class LoginAsRestaurant extends AppCompatActivity implements View.OnClickListener { 

    EditText lRestName; 
    EditText lRestPass; 
    CheckBox saveLoginCheckBox; 
    private SharedPreferences loginAsRestPreferences; 
    private Boolean saveLogin; 
    private SharedPreferences.Editor loginPrefsEditor; 
    String username, password; 

//__________________onCreate Method________________// 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.loginasrestaurant); 
     //refer to views 
     lRestName = (EditText) findViewById(R.id.RestUsername); 
     lRestPass = (EditText) findViewById(R.id.RestPassword); 
     final Button loginAsRest = (Button)findViewById(R.id.loginAsRestaurant); 
     saveLoginCheckBox = (CheckBox) findViewById(R.id.RestRememberCust); 
     loginAsRestPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); 
     loginPrefsEditor = loginAsRestPreferences.edit(); 
     /////////////////////////////Rememberme///////////////////////////////////////////// 
     saveLogin = loginAsRestPreferences.getBoolean("saveLogin", false); 
     if (saveLogin == true) { 

      lRestName.setText(loginAsRestPreferences.getString("username", "")); 
      lRestPass.setText(loginAsRestPreferences.getString("password", "")); 
      saveLoginCheckBox.setChecked(true); 
     } 
}//end of onCreate method 
//_________________________________________________________________________// 
     public void onClick(View v){ 
    if (saveLoginCheckBox.isChecked()) { 
      loginPrefsEditor.putBoolean("saveLogin", true); 
      loginPrefsEditor.putString("username", username); 
      loginPrefsEditor.putString("password", password); 
      loginPrefsEditor.commit(); 
     } else { 
      loginPrefsEditor.clear(); 
      loginPrefsEditor.commit(); 
     } 
}//end of onClick method 

}//end of class 

這是從登錄的代碼段客戶:

public class LoginAsCustomer extends Activity implements View.OnClickListener { 
    // //////////////////////Widgets in Login Tab////////////////////////////// 
    EditText lCustUserName; 
    EditText lCustPassword; 
    CheckBox saveLoginCheckBox; 
    private SharedPreferences loginPreferences; 
    private SharedPreferences.Editor loginPrefsEditor; 
    private Boolean saveLogin; 




    String username, password; 
//_______________________________________________________// 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.loginascustomer); 
    ///////////////////////// Widgets definition (Login Tab)///////////// 
     lCustUserName = (EditText) findViewById(R.id.CustUsername); 
     lCustPassword = (EditText) findViewById(R.id.CustPassword); 
     final Button loginAsCustomer = (Button) findViewById(R.id.loginAsCustomer); 
     saveLoginCheckBox = (CheckBox)findViewById(R.id.RememberCust); 
     loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); 
     loginPrefsEditor = loginPreferences.edit(); 

/////////////////////////////////////////////////////////////////////////////////////////////// 
     saveLogin = loginPreferences.getBoolean("saveLogin", false); 
     if (saveLogin == true) { 

      lCustUserName.setText(loginPreferences.getString("username", "")); 
      lCustPassword.setText(loginPreferences.getString("password", "")); 
      saveLoginCheckBox.setChecked(true);} 
}//end of onCreate method 
@Override 
    public void onClick(View v) { 

     if (saveLoginCheckBox.isChecked()) { 
      loginPrefsEditor.putBoolean("saveLogin", true); 
      loginPrefsEditor.putString("username", username); 
      loginPrefsEditor.putString("password", password); 
      loginPrefsEditor.commit(); 
     } else { 
      loginPrefsEditor.clear(); 
      loginPrefsEditor.commit(); 
     } 
}//end of onClick method 
}end of class 
+0

問題並不清楚。你的目標是什麼? –

+1

該應用程序有兩個登錄頁面:一個用於客戶,另一個用於餐廳,每個登錄頁面都記住了我的複選框,當我想以客戶身份登錄並保存我的憑據時,我不必每次使用時都輸入它們,我點擊複選框(真),它應該只在客戶登錄頁面顯示憑據,但它顯示在客戶和餐廳,反之亦然,當我登錄爲餐廳時,希望這是更具體的 –

回答

0

這是因爲這兩個類具有相同的首選項名稱「saveLogin」。使用獨特的名字就是這樣,

在LoginAsRestaurant類

saveLogin = loginPreferences.getBoolean("customerLogin", false); 

在LoginAsCustomer類

saveLogin = loginPreferences.getBoolean("restaurentLogin", false); 
+0

謝謝!運作良好! –

相關問題