当我单击搜索输入字段时,Android SearchView 仅会短暂显示光标,而不会显示键盘无法输入文字

问题描述

我正在尝试在布局内实现 SearchView,而不是在菜单布局中,但是当我单击搜索输入框时,光标只是短暂显示,然后消失,并且键盘不显示。似乎它可能会失去焦点?我一直在尝试解决这个问题,但运气为零。

代码如下。为清楚起见已进行编辑。

感谢您的任何想法或建议。

使用 SearchView 进行布局

html {
  background:#494949;
}

html::before,html::after {
  content:"";
  position:fixed;
  top:0;
  left:0;
  right:0;
  bottom:0;
}

html::before {
  --c:radial-gradient(circle 10px at 10px 10px,black 90%,transparent);
  background: var(--c) 0 0,var(--c) 20px 25px;
  background-size:40px 50px;
  filter:drop-shadow(-2px 2px 2px #fff);
}
html::after {
  background:radial-gradient(transparent,#040404);
}

activity_main

<androidx.constraintlayout.widget.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:id="@+id/LinearLayout1"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        app:iconifiedByDefault="false"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="always|collapseActionView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.163"
        app:queryHint="Search Here" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list_slidermenu"
        android:layout_width="313dp"
        android:layout_height="362dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchView"
        app:layout_constraintVertical_bias="0.32" />


</androidx.constraintlayout.widget.ConstraintLayout>

主活动

<?xml version="1.0" encoding="utf-8"?>

<!--the root view must be the DrawerLayout-->
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity"
    tools:ignore="HardcodedText">


    <androidx.constraintlayout.widget.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:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        tools:context=".MainActivity">




    </androidx.constraintlayout.widget.ConstraintLayout>


    <!--this the navigation view which draws
          and shows the navigation drawer-->
    <!--include the menu created in the menu folder-->
    <!--app:menu="@menu/navigation_menu"  this goes in nav view if needed-->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="350dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:fitsSystemWindows="true"


    android:clickable="true"
    >

        <include
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/some_id_if_needed"
            layout="@layout/nav_choose_foods" />
</com.google.android.material.navigation.NavigationView>


</androidx.drawerlayout.widget.DrawerLayout>

解决方法

嗯,我刚刚想通了这一点。正是“android:descendantFocusability="blocksDescendants”这一行把事情搞砸了。不知道我是怎么错过的。