如何在 Android 上禁用/管理 ImageView 的辅助功能通告?

3

我的屏幕有图像和标题视图。当页面加载时,我需要将TalkBack公告作为标题文本。但它总是宣布ImageView的contentDescription。

视图层次结构

ImageView然后是标题。

我尝试了什么?

尝试将焦点设置在标题上, 为此,我将这些属性设置为ImageView

android:focusable="false"
android:accessibilityTraversalAfter="@id/title"
android:importantForAccessibility="no"

很不幸,这些方法在我的情况下都没有奏效。作为第二次尝试,我将它们添加到我的片段中。

onResume {

    if (accessibilityManager?.isEnabled == true) {
        activity.title = title
        activity.window.decorView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED)
    }
}

还是不起作用!! ImageView 的 contentDescription 有这么大的能量吗?

加载页面时需要宣布 标题 文本,如果用户点击(或向左/向右滑动),则 ImageView 的描述需要被宣布。

XML ....

<data>

.....

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/vertical_padding"
    android:theme="@style/CL.Theme.Light">

    <ImageView
        android:id="@+id/close_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_close"
        android:importantForAccessibility="no"
        android:contentDescription="CLOSE"
        android:accessibilityTraversalAfter="@id/title"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/back_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="BACK"
        android:src="@drawable/icon_back"
        android:importantForAccessibility="no"
        android:accessibilityTraversalAfter="@id/title
        android:layout_margin="@dimen/icon_margin"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.core.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/back_button"
        app:layout_constraintBottom_toTopOf="@id/primary_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top|clip_horizontal"
            android:paddingStart="@dimen/horizontal_padding"
            android:paddingTop="@dimen/error_screen_top_offset"
            android:paddingEnd="@dimen/horizontal_padding"
            android:paddingBottom="@dimen/vertical_padding">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/margin_top"
                android:gravity="center"
                android:text="HEADER TITLE"
                android:focusable="true" 
                android:focusableInTouchMode="true"
                android:importantForAccessibility="yes"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_goneMarginTop="62dp"
                tools:text="Title" />

            <TextView
                android:id="@+id/subtitle
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/subtitle_margin_top"
                android:gravity="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/title"
                tools:text="Subtitle" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/cta_margin_top"
        android:layout_marginStart="@dimen/horizontal_padding"
        android:layout_marginEnd="@dimen/horizontal_padding"
        android:layout_marginBottom="@dimen/secondary_cta_margin"
    />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="@dimen/horizontal_padding"
        android:layout_marginEnd="@dimen/horizontal_padding"
        android:gravity="center"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

你能分享一下 XML 布局的例子吗? - sigute
@sigute 添加了XML布局。ImageViews是close_button和back_button。Talkback宣布了内容的描述。 - Joyal
在xml中交换视图可能有效,先声明NestedScrollView,然后在下面放置按钮...但是你是否考虑过使用Toolbar而不是尝试制作自己的呢? :) 它会默认具有您要实现的行为。 - sigute
@sigute 我尝试了交换视图,但是没有起作用。这是一个对话框片段。工具栏在我的情况下不是一个选项。 - Joyal
1个回答

0

从不同的StackOverflow 问题 中找到了更好的解决方案。

希望这能帮助到其他遇到类似问题的人。

fun View?.requestAccessibilityFocus(): View? {
   this?.performAccessibilityAction(ACTION_ACCESSIBILITY_FOCUS, null)
   this?.sendAccessibilityEvent(TYPE_VIEW_SELECTED)
   return this
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接