Adding support for settings screen android
I'am using several CheckBoxPr开发者_JS百科eference in my android app to add a settings menu. But is there a way to say that one and only one CheckBoxPreference MUST be checked at the time?
Here's one way to do it, but it could certainly be improved!
public class Settings extends PreferenceActivity{
CheckBoxPreference cb1;
CheckBoxPreference cb2;
CheckBoxPreference cb3;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
addPreferencesFromResource(R.xml.preferences);
cb1 = (CheckBoxPreference) findPreference("CheckBox1");
cb2 = (CheckBoxPreference) findPreference("CheckBox2");
cb3 = (CheckBoxPreference) findPreference("CheckBox3");
cb1.setOnPreferenceChangeListener(new Listener());
cb2.setOnPreferenceChangeListener(new Listener());
cb3.setOnPreferenceChangeListener(new Listener());
}
private class Listener implements OnPreferenceChangeListener {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if((Boolean) newValue)
removeOthers(preference.getKey());
return true;
}
}
private void removeOthers(String key) {
if(key.equals("CheckBox1")) {
cb2.setChecked(false);
cb3.setChecked(false);
}
else if(key.equals("CheckBox2")) {
cb1.setChecked(false);
cb3.setChecked(false);
}
else if(key.equals("CheckBox3")) {
cb1.setChecked(false);
cb2.setChecked(false);
}
}
}
This is pretty basic logic:
int numOfCheck = 0;
//Check each checkbox
if(checkbox1.checked){
numOfCheck+=1;
}
//... Add other checkboxes
if(numOfCheck>1||numOfCheck<1){
//... Alert the user
}
It's simple: RadioGroup.
精彩评论