android togglebutton 样式,button - Android ToggleButton always check - Stack Overflow

邢勇
2023-12-01

I want to store in SharedPreferences if the toggle button is Checked or uncheck.

toggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)

if(isChecked){

SharedPreferences.Editor editor = getSharedPreferences("SharedPrefName", MODE_PRIVATE).edit();

editor.putBoolean("check", true);

editor.commit();

}

else {

SharedPreferences.Editor editor = getSharedPreferences("SharedPrefName", MODE_PRIVATE).edit();

editor.putBoolean("nocheck", false);

editor.commit();

}

}});

But when I test this, it is always set to check

in OnCreate() I added this:

SharedPreferences preferences = getSharedPreferences("SharedPrefName", MODE_PRIVATE);

boolean value1 = preferences.getBoolean("check", true);

if (value1 = sharedPrefs.getBoolean("check", true)){

psw.setChecked(value1);

}else {

psw.setChecked(false);

}

 类似资料: