2016-04-27 7 views
0

「Remember Me」チェックボックスをオンにした場合、アプリは名前と姓を覚えていないのはなぜ分かりません。私はおそらく何かを忘れてしまったが、実際に何かを見つけることはできない。私は半分働いていることを覚えています - 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

あなたの変数の中にスペースを入れてはいけないかもしれません。 "LastName"を試してください。 – durbnpoisn

+1

pro-tip:キーの定数を定義します。それはあなたにそのような多くの問題を避けるでしょう。 – njzk2

答えて

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

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

「N」は、ここで

関連する問題