我有一个复选框首选项,我想从另一个复选框设置它的值。现在我可以从复选框首选项中获取值并将其设置为外部复选框,但我不知道如何从该外部复选框中设置首选项值。请帮助这是我的代码
getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); musicpref = getPrefs.getBoolean("musicp", true);
CheckBox musiccheck = (CheckBox) findViewById(R.id.checkBox1);
//The external Checkbox, is checked will start service , if not will stop it
musiccheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Intent musicstart = new Intent(MainActivity.this,
ServiceMusic.class);
startService(musicstart);
} else {
Intent musicstart = new Intent(MainActivity.this,
ServiceMusic.class);
stopService(musicstart);
}
}
});
// Preference checkbox , if is true will set the external checkbox to true
if (musicpref == true) {
musiccheck.setChecked(true);
}else{
musiccheck.setChecked(false);
}