1

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

});

この後どうしたらいいのかわからない。ラジオボタンのリストを表示する環境設定をクリックするコードを投稿できますか? どうもありがとうございました

4

1 に答える 1

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>

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" />
于 2013-07-10T05:01:02.950 に答える