checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
editText.setText(nm);
editText1.setText(ps);
Toast.makeText(Page1.this,"Checkbox Remember me is " + String.valueOf(b),Toast.LENGTH_LONG).show();
}
}
});
我正在尝试创建一个包含两个 EditText 和一个复选框的登录页面。当选中复选框时,我试图将登录详细信息保存在共享首选项中。在我的代码中,我设置了 OnCheckedListner 来了解它是否被检查。但 boolean 值 b
始终为 true...为什么?
请帮助我!
Page1 Activity 的完整代码:
public class Page1 extends AppCompatActivity {
Button button;
EditText editText,editText1;
CheckBox checkBox;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
button = (Button)findViewById(R.id.bt1);
editText = (EditText)findViewById(R.id.et1);
editText1 = (EditText)findViewById(R.id.et2);
checkBox = (CheckBox)findViewById(R.id.cb1);
sharedPreferences = getSharedPreferences("MYSP", MODE_PRIVATE);
final String nm = sharedPreferences.getString("uname", "");
final String ps = sharedPreferences.getString("upass", "");
final boolean saveLogin = sharedPreferences.getBoolean("save",false);
final boolean logout = sharedPreferences.getBoolean("logout", false);
if (!logout){
Intent intent = new Intent(Page1.this, ProfilePage.class);
startActivity(intent);
}
final boolean[] isChecked = new boolean[1];
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
editText.setText(nm);
editText1.setText(ps);
Toast.makeText(Page1.this,"Checkbox Remember me is "+ String.valueOf(b) ,Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(Page1.this,"Checkbox Remember me is "+ String.valueOf(b),Toast.LENGTH_LONG).show();
}
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editText.getText().toString();
String pass = editText1.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
if (isChecked[0]) {
editor.putString("uname", name);
editor.putString("upass", pass);
editor.putBoolean("save", true);
editor.putBoolean("logout", false);
editor.commit();
}
else {
editor.putString("uname", name);
editor.putString("upass", pass);
editor.putBoolean("logout", false);
editor.commit();
}
Intent intent = new Intent(Page1.this, ProfilePage.class);
startActivity(intent);
}
});
}
}
个人资料页面 Activity 代码:
public class ProfilePage extends AppCompatActivity {
Button button;
TextView textView;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_page);
button = (Button)findViewById(R.id.btn1);
textView = (TextView)findViewById(R.id.tv1);
sharedPreferences = getSharedPreferences("MYSP", MODE_PRIVATE);
final String n = sharedPreferences.getString("uname", "");
final String p = sharedPreferences.getString("upass", "");
textView.setText("Welcome "+ n + " ");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedPreferences.edit();
if (sharedPreferences.getBoolean("save", false)) {
editor.putString("uname", n);
editor.putString("upass", p);
editor.putBoolean("logout", true);
editor.commit();
}
else {
editor.clear();
editor.putBoolean("logout", true);
editor.commit();
}
textView.setText("Logout Success!");
}
});
}
@Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
}
最佳答案
您的代码仅处理b = true
的情况。修改您的代码以包含 else
部分,例如
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
editText.setText(nm);
editText1.setText(ps);
Toast.makeText(Page1.this,"Checkbox Remember me is true",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Page1.this,"Checkbox Remember me is false",Toast.LENGTH_LONG).show();
}
}
});
关于java - Android CheckBox OnCheckListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45453222/