记住我在登录时的功能

时间:2011-11-22 15:13:35

标签: android

我有两个文本框和一个checkmebox。但是当我输入我的用户名和密码并在复选框上打勾时,退出应用程序并再次返回它不会出现。为什么呢?

// Get reference to UI elements
        txtLogin = (EditText) findViewById(R.id.txtLogin);
        txtPassword = (EditText) findViewById(R.id.txtPassword);

        SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   
        String username = pref.getString(PREF_USERNAME, null);
        String password = pref.getString(PREF_PASSWORD, null);

        if (username == null || password == null) {
            //Prompt for username and password
            Toast.makeText(getBaseContext(),
                    "HI",
                    Toast.LENGTH_SHORT).show();
        }


        // Remember me function
        CheckBox cbRemember = (CheckBox) findViewById(R.id.chkRememberPassword);
        if (cbRemember.isChecked()) {

            getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
            .edit()
            .putString(PREF_USERNAME, txtLogin.toString())
            .putString(PREF_PASSWORD, txtPassword.toString())
            .commit();
        }

2 个答案:

答案 0 :(得分:1)

首先:您不应该使用txtLogin.toString();,而是使用txtLogin.getText();从editText控件中获取值。

您确定调用了保存首选项的代码吗?

答案 1 :(得分:0)

使用这些来获取价值

   String username = txtLogin.getText().toString().trim();