首页 > 解决方案 > 创建单选风格首选项

问题描述

我正在尝试创建一个 Radio 样式Preference,用户可以在其中单击一个选项,然后将其分配为的新值Preference

在此处输入图像描述

但是,当我单击一个选项时,会选择所有 3 个选项:

在此处输入图像描述

这是我的布局:

首选项.xml

<Preference
    android:layout="@layout/preference_gender"
    android:key="seeking"
    android:title="Seeking"/>

偏好性别.xml

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/seeking"
        android:singleLine="true"
        android:padding="10dp"
        android:textSize="20sp"
        android:textColor="@color/colorIcons"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/male"/>

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/male"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/female"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="@dimen/settings_textSize"
        android:text="Male"
        android:gravity="center"
        android:background="@drawable/settings_gender"
        android:padding="@dimen/settings_box_selection_padding"
        android:paddingStart="@dimen/button_horizontal_padding"
        android:paddingEnd="@dimen/button_horizontal_padding"
        android:textColor="@color/colorPrimaryDark"/>

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/female"
        app:layout_constraintStart_toEndOf="@+id/male"
        app:layout_constraintEnd_toStartOf="@id/both"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="@dimen/settings_textSize"
        android:text="Female"
        android:gravity="center"
        android:padding="@dimen/settings_box_selection_padding"
        android:background="@drawable/settings_gender"
        android:paddingStart="@dimen/button_horizontal_padding"
        android:paddingEnd="@dimen/button_horizontal_padding"
        android:textColor="@color/colorPrimaryDark"/>

    <androidx.appcompat.widget.AppCompatTextView
        app:layout_constraintStart_toEndOf="@id/female"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/both"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="@dimen/settings_textSize"
        android:text="Both"
        android:gravity="center"
        android:padding="@dimen/settings_box_selection_padding"
        android:background="@drawable/settings_gender"
        android:paddingStart="@dimen/button_horizontal_padding"
        android:paddingEnd="@dimen/button_horizontal_padding"
        android:textColor="@color/colorPrimaryDark"/>

</androidx.constraintlayout.widget.ConstraintLayout>

所以我希望能够单击一个文本视图,然后在Preference.OnPreferenceClickListener.

知道怎么做吗?

编辑:

class PreferencesFragment : PreferenceFragmentCompat(), View.OnClickListener {   

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    val genderPreference: Preference = findPreference("seeking")!!
    val parentView = listView.getChildAt(genderPreference.order)
    val childView = parentView.findViewById<TextView>(R.id.male) // NullPointerException
    }

    override fun onClick(v: View?) {
        Log.d(TAG, "clicked") // doesn't log
        when (v?.id){
            R.id.male -> Log.d(TAG, "male")
        }
    }

}

标签: androidxmlkotlin

解决方案


这可能是因为所有三个按钮都在一个首选项下,当您设置 时OnPreferenceClickListener,它会选择整个首选项以及您作为一个整体包含的布局。

因此,OnPreferenceClickListener您应该尝试获取父视图并在这些TextViews 上单独设置点击侦听器,然后从那里更新首选项的值,而不是设置一个。

或者您可以将布局 xml 文件中onClick的每个属性设置TextView为所需的功能。

要获得此首选项的视图,您可以使用类似

View preferenceView = getListView().getChildAt(preference.getOrder());

或者,使用 getView 方法并为这两个参数提供 null:

View preferenceView = preference.getView(null, null);

如果您使用 AndroidX 库作为偏好,则没有getView方法,因此唯一的选择是创建自定义 Preference 类并处理那里的视图:

public class CustomPreference extends Preference {
    public CustomPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onBindView(View rootView) {
        super.onBindView(rootView);

        View myView = rootView.findViewById(R.id.myViewId);
        // get your textViews from myView
        TextView maleText = myView.findViewById(R.id.male);
        ...
    }
}

然后在你的preference.xml中使用它,如下所示:

<your.package.CustomPreference
    android:key="your_key"
    android:layout="@layout/your_layout" />

推荐阅读