2016-04-27 102 views
0

我不明白爲什麼如果我點擊複選框「記住我」,該應用程序將只記住名稱而不是姓氏。我可能忘記了一些東西,但我實際上找不到。記住我一半工作 - Android

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 

    c = this; 

    etName = (EditText) findViewById(R.id.nameEditText); 
    etLastName = (EditText) findViewById(R.id.lastNameEditText); 
    etTavolo = (EditText) findViewById(R.id.tavoloEditText); 
    etPassword = (EditText) findViewById(R.id.passwordEditText); 
    btnLogin = (Button) findViewById(R.id.loginBtn); 
    checkBox = (CheckBox)findViewById(R.id.cbLogin); 

    loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); 
    loginPrefsEditor = loginPreferences.edit(); 
    saveLogin = loginPreferences.getBoolean("saveLogin", false); 

    if (saveLogin == true) { 
     etName.setText(loginPreferences.getString("Name", "")); 
     etLastName.setText(loginPreferences.getString("Last Name", "")); 
     checkBox.setChecked(true); 
    } 


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

      //REMEMBER ME CHECK BOX 
      if (v == btnLogin) { 
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(etName.getWindowToken(), 0); 

       String name = etName.getText().toString(); 
       String lastName = etLastName.getText().toString(); 
       String tavolo = etTavolo.getText().toString(); 

       if (checkBox.isChecked()) { 
        loginPrefsEditor.putBoolean("saveLogin", true); 
        loginPrefsEditor.putString("Name", name); 
        loginPrefsEditor.putString("Last name", lastName); 
        loginPrefsEditor.commit(); 
       } else { 
        loginPrefsEditor.clear(); 
        loginPrefsEditor.commit(); 
       } 

其餘代碼不需要這個問題。

+0

你確定要裝入的最後一個名字打開時的首選項嗎? –

+0

也許不要在變量中加一個空格。嘗試「姓氏」。 – durbnpoisn

+1

pro-tip:爲你的密鑰定義常數。這將避免你很多這樣的問題。 – njzk2

回答

0
etLastName.setText(loginPreferences.getString("Last Name", "")); 

loginPrefsEditor.putString("Last name", lastName); 

「N」是不同的情況下,這裏