创建无线电风格的首选项

时间:2019-05-22 23:03:20

标签: android xml kotlin

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

enter image description here

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

enter image description here

这是我的布局:

preferences.xml

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

preference_gender.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")
        }
    }

}

1 个答案:

答案 0 :(得分:0)

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

因此,与其设置OnPreferenceClickListener,不如尝试获取父视图,并分别在这TextView上设置点击侦听器,然后从那里更新首选项的值。

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

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

TextView

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

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

如果您使用AndroidX库作为首选项,则没有View preferenceView = preference.getView(null, null); 方法,因此唯一的选择是创建自定义的Preference类并在其中处理视图:

getView

然后像下面这样在您的preference.xml中使用它:

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);
        ...
    }
}
相关问题