我验证登录页面是由服务器端完成的。我创建一个复选框以记住用户名和密码。如果选中复选框则会记住用户名和密码。但问题是即使取消选中复选框也会发现存储用户名和密码。现在我想如果取消选中复选框意味着它不能记住用户名和密码
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();
}
}
});
答案 0 :(得分: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();
}
答案 1 :(得分: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();
}
}
});