java - 单击 RadioGroup 时如何显示首选项

标签 java android xml

我想创建一个 RadioButtons 列表来单击首选项。我用这种方式做了单选按钮:

<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() {

});

之后我不知道该怎么办。您可以发布代码来单击首选项显示单选按钮列表吗?非常感谢

最佳答案

没有现成可用的 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>

创建将扩展 DialogPreference 的 RadioGroupPreference 类。

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" />

关于java - 单击 RadioGroup 时如何显示首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17562608/

相关文章:

java - cardview :)? 内的灰线

java - 当数据库更改时,前一个 String 键会发生 NumberFormatException

java - 如何使我的 JSP 项目更易于设计人员使用

android - 在 Phonegap 中读取 URL/Google Docs/XML

python - 如何为具有特定属性值的xml元素选择数据,并根据key区分多个元素?

java - 在运行时访问strings.xml进行解析

java - 如何包装库类的迭代器以捕获 NoSuchElementException

java - Google API - getLastKnownLocation 的 NULL 指针

android - 如何在 Android 中找到形状的旋转点

java - 将自定义属性添加到现有 View