checkboxpreference android 中的多重依赖

发布于 2024-10-18 06:17:34 字数 64 浏览 4 评论 0原文

我的首选项屏幕中有三个复选框。我想让用户一次只选择一个复选框。 我该如何实现这一目标?

先感谢您。

i have three checkboxes in my preference screen. i want to make the user select only one checkbox at a time.
How do i achieve this?

thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾听心声的旋律 2024-10-25 06:17:34

您需要创建一个 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;    
                }
        };

然后使用它,

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

或者,您可以使用 ListPreference 它将弹出一个单选组,您可以将三个选项放在那里。这就是我在我的应用程序中所做的,但是如果需要复选框首选项,那么这当然可以工作。

这是我的列表首选项(用于选择响铃时长):

<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" />

这是我的 @array/ringTime 和 @array/ringSetting 的 XML

 <?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(); 

You would 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 a ListPreference which will pop up a radio group and you can just put the three option in there. That's what I did in my App, but If the checkboxpreferences are needed, then that of course will work.

Here's my listpreference (for choosing how long it will ring for):

<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's my XML for the @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>

I know this is a 5-month old question as of today, but I thought this might help someone else. :)

And then for implementing the listpreference inside my code:

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(); 
掀纱窥君容 2024-10-25 06:17:34

也许使用无线电组? :)
http://developer.android.com/resources/tutorials/views /hello-formstuff.html#RadioButtons

或者如果您确实需要使用复选框小部件,您可以使用 isChecked() 检查其他复选框,然后在 OnCheckedChangeListener 类实现中通过 setChecked(false) 取消选中

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

or if you really need to use checkbox widget, you could check other checkboxes using isChecked() and untick then via setChecked(false) in your OnCheckedChangeListener class implementation

め可乐爱微笑 2024-10-25 06:17:34
@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);
}
@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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文