How to create RadioButton group in preference.xml window?

前端 未结 4 1920
广开言路
广开言路 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 18:57

    I think this is pretty simple if that is what you want:

    1. Add the checkboxes at the preferenses xml like that:

       
      
      
      
      //etc...
      
    2. At the preferences class onCreate build an CheckBoxPreference array or list or whatever you like, like that:

      ArrayList cbp_list = new ArrayList();
      cbp_list.add((CheckBoxPreference) getPreferenceManager()
              .findPreference(PreferencesProtocol.prefkey_cbp_1));
      cbp_list.add((CheckBoxPreference) getPreferenceManager()
              .findPreference(PreferencesProtocol.prefkey_cbp_2));
      
    3. Make your preferences class implement OnPreferenceClickListener and set the OnPreferenceClickListener to the checkboxes like that :

      for (CheckBoxPreference cbp : cbp_list) {
      cbp.setOnPreferenceClickListener(this);
      }
      
    4. Then just override the onPreferenceClick and handle any click. For example if you want checkboxes to perform like radio buttons (in the same radiogroup), meaning, only one checkbox at the time to be check, do something like that:

      @Override
      public boolean onPreferenceClick(Preference arg0) {
      
      for (CheckBoxPreference cbp : cbp_list) {
      
          if (!cbp.getKey().equals(arg0.getKey()) && cbp.isChecked()) {
              cbp.setChecked(false);
          }
      
      }
      
      return false;
      }
      

提交回复
热议问题