0

我正在验证登录页面是由服务器端完成的。我创建一个记住用户名和密码的复选框。如果选中复选框,它会记住用户名和密码。但问题是即使未选中复选框,它也会找到存储的用户名和密码. 现在我想如果复选框未选中意味着它不记得用户名和密码

 i try this code
   public class Login extends Activity {
  SharedPreferences app_preferences ;
CheckBox check;


private static final String UPDATE_URL = "serverurl";

public ProgressDialog progressDialog;

private EditText UserEditText;

private EditText PassEditText;



public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
    progressDialog = new ProgressDialog(this);

    progressDialog.setMessage("Please wait...");

    progressDialog.setIndeterminate(true);

    progressDialog.setCancelable(false);



    UserEditText = (EditText) findViewById(R.id.username);

    PassEditText = (EditText) findViewById(R.id.password);
    check=(CheckBox) findViewById(R.id.checkbox);
    String Str_user = app_preferences.getString("username","0" );
    String Str_pass = app_preferences.getString("password", "0");
    String Str_check = app_preferences.getString("checked", "no");
    if(Str_check.equals("yes"))
    {
            UserEditText.setText(Str_user);
           PassEditText.setText(Str_pass);
            check.setChecked(true);
    }
    Button button = (Button) findViewById(R.id.okbutton);

    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            int usersize = UserEditText.getText().length();

            int passsize = PassEditText.getText().length();

            if(usersize > 0 && passsize > 0) {

                progressDialog.show();

                String user = UserEditText.getText().toString();

                String pass = PassEditText.getText().toString();
                String Str_check2 = app_preferences.getString("checked", "no");
                if(Str_check2.equals("yes"))
                {
                    SharedPreferences.Editor editor = app_preferences.edit();
                    editor.putString("username", user);
                    editor.putString("password", pass);
                     editor.commit();
                }

                doLogin(user, pass);

            } else createDialog("Error","Please enter Username and Password");

        }

    });

   check box
    check.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                // Perform action on clicks, depending on whether it's now checked
                SharedPreferences.Editor editor = app_preferences.edit();
                if (((CheckBox) v).isChecked())
                {

                     editor.putString("checked", "yes");
                     editor.commit();
                }
                else
                {
                     editor.putString("checked", "no");
                     editor.commit();
                }
            }
        });
4

2 回答 2

0

当用户取消选中该复选框时,您应该删除存储的详细信息。即使用户未选中该复选框,您也可以在此处保留存储的值。

 check box
        check.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    // Perform action on clicks, depending on whether it's now checked
                    SharedPreferences.Editor editor = app_preferences.edit();
                    if (((CheckBox) v).isChecked())
                    {

editor.putString("username", user);
                    editor.putString("password", pass);
                         editor.putString("checked", "yes");
                         editor.commit();
                    }
                    else
                    {

editor.putString("username", "");
                    editor.putString("password", "");
editor.putString("checked", "no");
                         editor.commit();
                    }
                }
            });
于 2012-04-20T05:34:56.463 回答
0

尝试

 String Str_check = app_preferences.getString("checked", "no");
    if(Str_check.equals("yes"))
    {
String Str_user = app_preferences.getString("username","0" );
    String Str_pass = app_preferences.getString("password", "0");

            UserEditText.setText(Str_user);
           PassEditText.setText(Str_pass);
            check.setChecked(true);
    }
else{
   SharedPreferences.Editor editor = app_preferences.edit();
                    editor.putString("username", "");
                    editor.putString("password", "");
                     editor.commit();
}
于 2012-04-20T04:47:04.863 回答