Android 自定义视图,其行为类似于 RadioButton/CheckBox

问题描述 投票:0回答:1

我有以下自定义视图 有 2 个文本字段和一个 RadioButton,我希望它的行为像 RadioButton(当涉及到对讲时)

目前: 当选择 customRadioButton 时,它会显示

文本1、文本2双击激活

因此它不会宣布组件的状态(选定/未选定)或类型。 当我导航到此自定义视图中的单选按钮视图时,它会正确宣布:未选择、testRadio、单选按钮

期望的行为: 它应该说(与 RadioButton 的结构相同)并将整个视图视为一个元素。

未选中,文本1,文本2,单选按钮,双击切换。

如何达到这样的效果? 另外,我希望能够在这个自定义元素的 RadioButton 或 Checkbox 行为之间切换。

custom_radio_button_with_text

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:id="@+id/textStack"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/radio_button"
        android:orientation="vertical">
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/radio_button"
            app:layout_constraintBottom_toTopOf="@id/text2"
            />

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/text1"
            />
    </LinearLayout>

    <RadioButton
        android:id="@+id/radio_button"
        android:text="testRadio"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/textStack"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



</androidx.constraintlayout.widget.ConstraintLayout>

CustomRadioButtonWithText
文件:

class CustomRadioButtonWithText(context: Context, attrs: AttributeSet) : ConstraintLayout(context,attrs), Checkable{

    private var isChecked = false
    private val radioButton: RadioButton
    private val textView1: TextView
    private val textView2: TextView

    init {
        LayoutInflater.from(context).inflate(R.layout.custom_radio_button_with_text, this, true)

        radioButton = findViewById(R.id.radio_button)
        textView1 = findViewById(R.id.text1)
        textView2 = findViewById(R.id.text2)

        setOnClickListener {
            toggle()
        }
    }

    override fun setChecked(checked: Boolean) {
        isChecked = checked
        radioButton.isChecked = isChecked
    }

    override fun isChecked(): Boolean {
        return isChecked
    }

    override fun toggle() {
        isChecked = !isChecked
        radioButton.isChecked = isChecked
    }
}

并且它用于以下

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.myapplication.CustomRadioButtonWithText
        android:id="@+id/customRadioButtonWithText"

        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </com.example.myapplication.CustomRadioButtonWithText>

            <androidx.appcompat.widget.AppCompatRadioButton
                android:text="standard radio button"
                android:id="@+id/standardRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="visible"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/customRadioButtonWithText"
                />

            <androidx.appcompat.widget.AppCompatCheckBox
                android:text="standard check box"
                android:id="@+id/standardCheckBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="visible"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/standardRadioButton"
                />

</androidx.constraintlayout.widget.ConstraintLayout>
android checkbox radio-button android-custom-view talkback
1个回答
0
投票

鉴于您的

CustomRadioButtonWithText
不从
RadioButton
扩展,我想不出一种方法可以使可访问性表现得像
RadioButton
一样,我建议在 RadioButton 上使用函数 announceForAccessibility您在自定义视图中拥有的实例。

© www.soinside.com 2019 - 2024. All rights reserved.