【问题标题】:AppBarLayout traps keyboard focusAppBarLayout 捕获键盘焦点
【发布时间】:2019-10-10 15:32:55
【问题描述】:

我正在努力确保键盘导航在我的应用上正常工作,并且当用户按下“Tab”键或“向上/向下”箭头键时,所有视图元素都可以聚焦。但是,AppBarLayout 似乎捕获了键盘焦点。我在配置了键盘的模拟器上运行我的应用程序。示例布局如下:

<android.support.constraint.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">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/search_fragment_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:orientation="vertical"
        android:scrollbars="horizontal"
        android:focusable="true"
        android:nextFocusDown="@id/recycler_view">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="Sample text 1"
                android:focusable="true"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="Sample text 2"
                android:focusable="true"/>
        </LinearLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:focusable="true"
        app:layout_constraintTop_toBottomOf="@layout/content_main"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

点击 Tab 键或向上/向下箭头不会将焦点切换到回收器视图,并且焦点停留在 AppBarLayout 中。在 textviews 上设置 nextFocusDown 属性并将视图元素设置为保持焦点也不起作用。 Here 是图片。我需要做什么才能使此布局易于访问?

【问题讨论】:

    标签: android accessibility


    【解决方案1】:

    我觉得你需要打电话

        findViewById(R.id.search_fragment_app_bar).setTouchscreenBlocksFocus(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            findViewById(R.id.search_fragment_app_bar).setKeyboardNavigationCluster(false);
        }
    

    setTouchscreenBlocksFocus

    setKeyboardNavigationCluster

    【讨论】:

    • setTouchscreenBlocksFocus 很有魅力,谢谢!有什么想法为什么会这样吗?来自文档:“设置此 ViewGroup 是否应忽略其自身及其子级的焦点请求。如果启用此选项并且 ViewGroup 或后代当前具有焦点,则焦点将继续前进。”但在我们的例子中,我们不需要忽略焦点,反之亦然!
    • 完美解决方案!这应该是公认的答案!
    • @whereismaxmax 当我们点击键盘上的 Tab 或箭头按钮时,ViewGroup 会收到一个焦点更改请求,并会调用focusSearch 方法来寻找下一个焦点视图。我认为如果 TouchscreenBlocksFocus 标志为真,AppBarLayout 将被视为“有效根”,它将忽略将焦点移至外部视图的请求。
    【解决方案2】:

    从 AppBarLayout 释放键盘焦点的方法是将实际设备(不是模拟器)连接到键盘并按下 Cmd+Tab(对于 Mac OS 用户)或 Windows+Tab(对于 Windows 用户)键。

    【讨论】:

    猜你喜欢
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多