我正在尝试确保键盘导航在我的应用程序上正常工作,并且当用户按下“ 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是图像。我需要怎么做才能使此布局可访问?
答案 0 :(得分: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);
}
答案 1 :(得分:0)
从AppBarLayout释放键盘焦点的方法是将实际设备(不是模拟器)挂接到键盘,然后按Cmd + Tab(对于Mac OS用户)或Windows + Tab(对于Windows用户)键。