Multiple dependency in checkboxpreference android

I have three checkboxes on my preferences screen. I want the user to select only one checkbox at a time. How do I achieve this?

thank you in advance.

+3
source share
4 answers

You will need to create an onpreferenceChangelistener:

prefChangeListener = new Preference.OnPreferenceChangeListener() {

                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (preference == pref1 && newValue.toString().equals("true")){
                        pref2.setChecked(false);
                        pref3.setChecked(false);
                    }
                    if (preference == pref2 && newValue.toString().equals("true")){
                        pref1.setChecked(false);
                        pref3.setChecked(false);
                    }
                    if (preference == pref3 && newValue.toString().equals("true")){
                        pref1.setChecked(false);
                        pref2.setChecked(false);
                    }
                    return true;    
                }
        };

Then, to use it,

pref1.setOnPreferenceChangeListener(prefChangeListener);
pref2.setOnPreferenceChangeListener(prefChangeListener);
pref3.setOnPreferenceChangeListener(prefChangeListener);

Or you can use the ListPreference, which will appear in the group of radio stations, and you can simply place three parameters there. This is what I did in my application, but if checkboxpreferences are needed, then this of course will work.

Here is my list (for determining how long he will call):

<ListPreference android:persistent="true"
            android:title="Ring Time"
            android:summary="Choose how long the phone will ring when activated"
            android:key="ringTime"
            android:defaultValue="ringNot"
            android:entries="@array/ringTime"
            android:entryValues="@array/ringSetting" />

And here is my XML for @ array / ringTime and @ array / ringSetting

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ringTime">
   <item>Don\'t Ring</item>
   <item>Ring 30 Seconds</item>
   <item>Ring 2 Minutes</item>   
   <item>Ring 5 Minutes</item>
</string-array>
<string-array name="ringSetting">
   <item>ringNot</item>
   <item>ring30Sec</item>
   <item>ring2Min</item>   
   <item>ring5Min</item>
</string-array>

</resources>

, 5- , , - .:)

listpreference :

ringTime = pref.getString("ringTime", "ringNot");//ListPreferences are stored as strings
if (ringTime.equals(ring30Sec))
//ring for 30 seconds
else if (ringTime.equals(ring2Min))
//ring for 2 minutes
else if (ringTime.equals(ring5Min))
//ring for 5 minutes
else Toast.makeText(getBaseContext(), "Not Ringing", Toast.LENGTH_LONG).show(); 
+2

.

+1

using the radio group? :) http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons

or if you really need to use the checkbox widget, you can check the other checkboxes with isChecked () and then cancel through setChecked (false) in your implementation of the OnCheckedChangeListener class

0
source
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    //mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    mLocationChangedListener = new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference,
                Object newValue) {
            mChbLocationLastChanged = (CheckBoxPreference) preference;
            return true;
        }

    };
    mLocationClickListener = new Preference.OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            setLocationAccuracy();
            return true;
        }
    };
    mChbLocationHigh = (CheckBoxPreference) findPreference("pref_chb_location_high_accuracy_key");
    mChbLocationHigh.setOnPreferenceChangeListener(mLocationChangedListener);
    mChbLocationHigh.setOnPreferenceClickListener(mLocationClickListener);
    mChbLocationBalanced = (CheckBoxPreference) findPreference("pref_chb_location_balanced_power_key");
    mChbLocationBalanced.setOnPreferenceChangeListener(mLocationChangedListener);
    mChbLocationBalanced
            .setOnPreferenceClickListener(mLocationClickListener);
    mChbLocationLowPower = (CheckBoxPreference) findPreference("pref_chb_location_low_power_key");
    mChbLocationLowPower.setOnPreferenceChangeListener(mLocationChangedListener);
    mChbLocationLowPower
            .setOnPreferenceClickListener(mLocationClickListener);
    mChbLocationNoPower = (CheckBoxPreference) findPreference("pref_chb_location_no_power_key");
    mChbLocationNoPower.setOnPreferenceChangeListener(mLocationChangedListener);
    mChbLocationNoPower
            .setOnPreferenceClickListener(mLocationClickListener);
}

private void setLocationAccuracy() {
    boolean isChecked = mChbLocationLastChanged.isChecked();
    String key = mChbLocationLastChanged.getKey();
    if (key.equalsIgnoreCase("pref_chb_location_high_accuracy_key")) {
        mChbLocationBalanced.setChecked(false);
        mChbLocationLowPower.setChecked(false);
        mChbLocationNoPower.setChecked(false);
    } else if (key.equalsIgnoreCase("pref_chb_location_balanced_power_key")) {
        mChbLocationHigh.setChecked(false);
        mChbLocationLowPower.setChecked(false);
        mChbLocationNoPower.setChecked(false);
    } else if (key.equalsIgnoreCase("pref_chb_location_low_power_key")) {
        mChbLocationHigh.setChecked(false);
        mChbLocationBalanced.setChecked(false);
        mChbLocationNoPower.setChecked(false);
    } else if (key.equalsIgnoreCase("pref_chb_location_no_power_key")) {
        mChbLocationHigh.setChecked(false);
        mChbLocationBalanced.setChecked(false);
        mChbLocationLowPower.setChecked(false);
    }
    if (!mChbLocationLastChanged.isChecked())
        mChbLocationLastChanged.setChecked(true);
}
0
source

Source: https://habr.com/ru/post/1793528/


All Articles