你怎么能RadioButtonPreference
在android中实现a?就像CheckBoxPreference
.
有什么解决方法吗?
使用ListPreference
. 您需要有多个单选按钮才能使其有意义(否则,它将只是一个复选框)。
如果出于某种原因,您不想使用 a ,请从 Android 开源树ListPreference
中获取源代码并滚动您自己的 Preference 实现。CheckBoxPreference
然后,您应该能够使用命名空间(例如 com.foo. RadioButtonPreference
)在首选 XML 中列出您的自定义实现。
您可以使用从以下扩展的自定义类CheckBoxPreference
:
public class RadioButtonPreference extends CheckBoxPreference {
public RadioButtonPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setWidgetLayoutResource(R.layout.preference_widget_radiobutton);
}
public RadioButtonPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setWidgetLayoutResource(R.layout.preference_widget_radiobutton);
}
public RadioButtonPreference(Context context) {
this(context, null);
}
@Override
public void onClick() {
if (this.isChecked()) {
return;
}
super.onClick();
}
}
然后创建一个preference_widget_radiobutton.xml
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout used by CheckBoxPreference for the checkbox style. This is inflated
inside android.R.layout.preference. -->
<RadioButton android:id="@+android:id/checkbox"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="false"
android:focusable="false"/>