记住我半工作 - Android

时间:2016-04-27 20:48:40

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

此问题不需要其余代码。

1 个答案:

答案 0 :(得分:0)

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

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

" N"这是不同的案例