一 .组合布局
1.继承你要重写的类。在里面添加,你需要添加你想要的方法。
(通过构造方法的反复调用)
public class Radiopreference extends CheckBoxPreference { private String mRadioGroup; public Radiopreference(Context context) { this(context, null); } public Radiopreference(Context context, AttributeSet attrs) { super(context, attrs); final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RadioPreference, 0, 0); mRadioGroup = a.getString(R.styleable.RadioPreference_radioGroup); a.recycle(); setWidgetLayoutResource(R.layout.radio_preference_widget); } public String getRadioGroup() { return mRadioGroup; } public void setRadioGroup(String radioGroup) { mRadioGroup = radioGroup; } public void clearOtherRadioPreferences(PreferenceGroup preferenceGroup) { if(preferenceGroup != null){ final int count = preferenceGroup.getPreferenceCount(); for (int i = 0; i < count; i++) { final Preference p = preferenceGroup.getPreference(i); if (!(p instanceof Radiopreference)) { continue; } final Radiopreference Radiopreference = (Radiopreference) p; if (!TextUtils.equals(getRadioGroup(), Radiopreference.getRadioGroup())) { continue; } if (TextUtils.equals(getKey(), Radiopreference.getKey())) { continue; } Radiopreference.setChecked(false); } }else{ Log.d("show", "preferenceGroup = null"); } } }2. 在XML中引用布局
<com.example.test.Radiopreference
/>