android - 旋转后软键盘消失
问题描述
我有一个简单的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/BrightYellowCrayola"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/tbDriver"
app:navigationIcon="@drawable/baseline_menu_24"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
>
<androidx.appcompat.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:defaultQueryHint="@string/search_drivers"
app:iconifiedByDefault="false"
app:searchIcon="@null"
app:queryBackground="@android:color/transparent"
app:submitBackground="@android:color/transparent"
/>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_refresh_layout_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="@dimen/zero_margin_when_normal"
android:paddingEnd="@dimen/zero_margin_when_normal" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/efab_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="@color/BrightYellowCrayola"
android:src="@drawable/baseline_person_add_24" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
问题是,在我的 serchfield 中输入文本并旋转手机后,searchview 会在短时间内保持焦点(键盘),然后再次消失,如本例所示
注意!:然而,“有趣”的事情是,只有当我水平旋转手机时才会发生这种情况,当我垂直定位手机时,软键盘会再次出现。
解决方案
尝试在您的搜索视图中添加以下行:
android:imeOptions="flagNoExtractUi"
所以最终结果应该是这样的:
<androidx.appcompat.widget.SearchView
android:id="@+id/svDriver"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:defaultQueryHint="@string/search_drivers"
app:iconifiedByDefault="false"
app:searchIcon="@null"
app:queryBackground="@android:color/transparent"
app:submitBackground="@android:color/transparent"
android:imeOptions="flagNoExtractUi"
/>