How to create RadioButton group in preference.xml window?

前端 未结 4 1922
广开言路
广开言路 2021-01-30 18:27

I\'m beginner in Java Android developing. I\'m using Eclipse SDK 3.6.1 version. I have a preferences window with two checkbox and one back button.

    

        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 19:09

    In some situations, having Preferences behave as radio buttons is a nice feature e.g a simple way of including summary text under each option.

    Making a group of checkboxes behave like a group of radio buttons is fairly simple. To do this in Preferences, register all the checkboxes in the group to the same OnPreferenceChangeListener then in this listener use findPreference() to find the other checkboxes and call setChecked(false);

    public boolean onPreferenceChange(Preference preference, Object newValue) {
        String key = preference.getKey();
        if (key.equals("checkboxPref")) {
            //Reset other items
            CheckBoxPreference p = (CheckBoxPreference)findPreference("checkboxPref2");
            p.setChecked(false);
        }
        else if (key.equals("checkboxPref2")) {
            //Reset other items
            CheckBoxPreference p = (CheckBoxPreference)findPreference("checkboxPref");
            p.setChecked(false);
        }
    
        //Force the current focused checkbox to always stay checked when pressed
        //i.e confirms value when newValue is checked (true) and discards newValue
        //when newValue is unchecked (false)
        return (Boolean)newValue;
    }
    

    The only downside is that the checkboxes will not look like radio buttons...

提交回复
热议问题