How to create RadioButton group in preference.xml window?

前端 未结 4 1919
广开言路
广开言路 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:08

    try this:

        @Override
        public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, @NonNull Preference preference) {
            CheckBoxPreference cb1 = null;
            CheckBoxPreference cb2 = null;
            ListAdapter adapter = preferenceScreen.getRootAdapter();
            for(int i = 0; i < preferenceScreen.getRootAdapter().getCount(); i++)
            {
                //attention to type and key
                Preference pref = (Preference) adapter.getItem(i);
                if (pref == null || pref.getKey() == null)
                    continue;
                if (pref.getKey().equals(getString(R.string.tag_preference_key_always_zoom_to_akt_position)))
                    cb1 = (CheckBoxPreference) pref;
                if (pref.getKey().equals(getString(R.string.tag_preference_key_always_zoom_to_last_location)))
                    cb2 = (CheckBoxPreference) pref;
                if (cb1 != null && cb2 != null)
                    break;
            }
            //be safe
            if (cb1 == null || cb2 == null)
                return super.onPreferenceTreeClick(preferenceScreen, preference);
    
            if (preference.getKey().equals(getString(R.string.tag_preference_key_always_zoom_to_akt_position))) {
                CheckBoxPreference cb = (CheckBoxPreference) preference;
                cb2.setChecked(!cb.isChecked());
            }
            if (preference.getKey().equals(getString(R.string.tag_preference_key_always_zoom_to_last_location))) {
                CheckBoxPreference cb = (CheckBoxPreference) preference;
                cb1.setChecked(!cb.isChecked());
            }
            return super.onPreferenceTreeClick(preferenceScreen, preference);
        }
    

提交回复
热议问题