2013-07-10 14 views
1

我想創建一個RadioButtons列表來點擊偏好設置。我做了這樣的單選按鈕:點擊RadioGroup如何顯示偏好

<RadioGroup> 
     <Preference android:key="ex" android:summary="Example" android:title="Example"/> 
    <RadioGroup 
      android:id="@+id/radioSex" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <RadioButton 
       android:id="@+id/radioMale" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/radio_male" 
       android:checked="true" /> 

      <RadioButton 
       android:id="@+id/radioFemale" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/radio_female" /> 

     </RadioGroup> 

後,Java文件

Preference ex; 
ex = (Preference) this.findPreference("ex"); 
ex.setOnPreferenceClickListener(new OnPreferenceClickListener() { 

}); 

在此之後,我不知道該怎麼辦英寸您可以發佈代碼以點擊首選項顯示單選按鈕列表?非常感謝

回答

0

沒有可用的開箱即用的RadioGroup首選項。您需要通過擴展DialogPreference類來編寫一個。

使用對話框內容創建單獨的佈局文件(/res/layout/pref_radiogroup.xml)。

<?xml version="1.0" encoding="UTF-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RadioGroup 
     android:id="@+id/radioSex" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/radioMale" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="true" 
      android:text="@string/radio_male" /> 

     <RadioButton 
      android:id="@+id/radioFemale" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/radio_female" /> 
    </RadioGroup> 

</FrameLayout> 

創建您的RadioGroupPreference類,它將擴展DialogPreference。

public class RadioGroupPreference extends DialogPreference { 

    private RadioGroup radioGroup; 

    public RadioGroupPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setDialogLayoutResource(R.layout.pref_radiogroup); 
    } 

    @Override 
    protected View onCreateDialogView() { 
     View root = super.onCreateDialogView(); 
     radioGroup = (RadioGroup) root.findViewById(R.id.radioSex); 
     return root; 
    } 

    @Override 
    protected void onBindDialogView(View view) { 
     super.onBindDialogView(view); 
     setSelectedValue(getPersistedString("Male")); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 
     super.onDialogClosed(positiveResult); 

     if (!positiveResult) { 
      return; 
     } 

     if (shouldPersist()) { 
      String valueToPersist = getSelectedValue(); 
      if (callChangeListener(valueToPersist)) { 
       persistString(valueToPersist); 
      } 
     } 
    } 

    @Override 
    protected Object onGetDefaultValue(TypedArray a, int index) { 
     return a.getString(index); 
    } 

    private void setSelectedValue(String value) { 
     for (int i = 0; i < radioGroup.getChildCount(); i++) { 
      View view = radioGroup.getChildAt(i); 
      if (view instanceof RadioButton) { 
       RadioButton radioButton = (RadioButton) view; 
       if (radioButton.getText().equals(value)) { 
        radioButton.setChecked(true); 
       } 
      } 

     } 
    } 

    private String getSelectedValue() { 
     int radioButtonId = radioGroup.getCheckedRadioButtonId(); 
     RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonId); 
     return (String) radioButton.getText(); 
    } 

} 

現在您只需將自定義首選項添加到您的首選項屏幕即可。

<com.package.RadioGroupPreference 
    android:summary="Radio Group Preference Summary" 
    android:title="Radio Group Preference" 
    android:key="preference_key" 
    android:defaultValue="@string/radio_male" /> 
+0

對不起,我不明白。我應該怎麼做?你可能會更精確,甚至可能會把示例代碼? –

+0

我剛剛編輯了我的答案並添加了一個代碼示例。 –