【问题标题】:Soft-keyboard dissapears after rotation旋转后软键盘消失
【发布时间】:2021-08-12 17:37:19
【问题描述】:

我有一个简单的片段布局:

<?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 会在短时间内保持焦点(键盘),然后再次消失,如 this example 所示

注意!: 然而,“有趣”的是,只有当我水平旋转手机时才会发生这种情况,当我垂直放置手机时,软键盘又会出现。

【问题讨论】:

    标签: android android-layout toolbar android-softkeyboard searchview


    【解决方案1】:

    尝试在您的搜索视图中添加以下行:

    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"
    
        />
    

    【讨论】:

      【解决方案2】:

      在您的 ma​​nifest.xml 文件中添加以下行

       <activity
                  android:name=".MyActivity"
                  android:windowSoftInputMode="stateUnchanged|adjustPan" >
      

      "stateUnchanged" 将使键盘的状态与之前的状态相同。隐藏或可见。

      “adjustResize” 将使您的编辑文本可见。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-30
        • 1970-01-01
        • 1970-01-01
        • 2013-03-16
        • 1970-01-01
        相关资源
        最近更新 更多