如何在 PreferenceScreen 中显示 RadioButton 列表(而不是在对话框中)

How to display a RadioButton list in PreferenceScreen (not in a Dialog)

单选按钮列表如何显示在 PreferenceSecreen 而不是对话框中?仅仅为了 select 此列表中的一项而不得不多次单击真的很烦人。

预期结果

当前结果

app_preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
            android:key="prefPhotoFilter"
            android:title="Photo filter"
            android:entries="@array/photoFilters" />

</PreferenceScreen>

strings.xml

<string-array name="photoFilters">
    <item name="1">Natural</item>
    <item name="2">Boosted</item>
    <item name="3">Saturated</item>
</string-array>

根据这个Gist, you can extend CheckBoxPreference并创建一个带有单选按钮的布局。

像这样: 首先,创建一个包含唯一单选按钮的新布局,我们称之为

preference_widget_radiobutton.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clickable="false"
        android:focusable="false" />

然后创建 CheckBoxPreference 的子类:

class RadioButtonPreference : CheckBoxPreference {

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) { setView() }

    constructor(context: Context, attrs: AttributeSet? = null) : super(context, attrs) { setView() }


    private fun setView(){
        widgetLayoutResource = R.layout.preference_widget_radiobutton
    }

    override fun onClick() {
        if (this.isChecked) 
            return

        super.onClick()
    }
}

在您的 app_preferences.xml 屏幕中:

    <PreferenceCategory android:title="Photo Filters">
        <Your_class_package_dirctory.RadioButtonPreference
                android:key="naturals"
                android:title="Natural" />
        <Your_class_package_dirctory.RadioButtonPreference
                android:key="boosted"
                android:title="Boosted" />
        <Your_class_package_dirctory.RadioButtonPreference
                android:key="saturated"
                android:title="Saturated" />
    </PreferenceCategory>

现在如您所见,这将像普通的 CheckBox 一样运行,它不会取消选中上一个单选按钮,解决此问题:

在您的首选项屏幕代码中:


class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceClickListener{
    private val sharedPreference = AppPreferences()


    private var oldCheckedPreference: RadioButtonPreference? = null

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.app_preferences, rootKey)

        findPreference<RadioButtonPreference>("naturals")?.apply {
            /*
            You can set the defualt button to be checked by:
              updateCheckedRadioButton(this)
            */
            onPreferenceClickListener = this@SettingsFragment
        }
        findPreference<RadioButtonPreference>("boosted")?.onPreferenceClickListener = this
        findPreference<RadioButtonPreference>("saturated")?.onPreferenceClickListener = this

    }

    private fun updateCheckedRadioButton(radioButtonPreference: RadioButtonPreference) {

        //Uncheck the previous selected button if there is.
        oldCheckedPreference?.isChecked = false
        radioButtonPreference.isChecked = true
        oldCheckedPreference = radioButtonPreference

    }

    override fun onPreferenceClick(preference: Preference): Boolean {
        if (preference is RadioButtonPreference)
            updateCheckedRadioButton(preference)

        return true
    }

}

结果是: