Android 当我点击搜索输入字段时,SearchView 只短暂显示光标,不显示键盘。无法输入文字

Android SearchView only briefly displays cursor when I click on search input field, and no keyboard displays. Can't input text

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

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

感谢任何想法或建议。

带有 SearchView 的布局

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

activity_main

<?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>

主要活动

public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter_AllFoods.ItemClickListener, RecyclerViewAdapter_Search.ItemClickListener, RecyclerViewAdapter_Nutrients.ItemClickListener, RecyclerViewAdapter_FoodTotals.ItemClickListener{

    public DrawerLayout drawerLayout;
    public ActionBarDrawerToggle actionBarDrawerToggle;

  SearchView searchView;


    //JSONObject finalSums = new JSONObject();

    static RecyclerViewAdapter_AllFoods adapter;

    static RecyclerViewAdapter_Nutrients adapter_nutrients;
    static RecyclerViewAdapter_FoodTotals adapter_foodtotals;

    static RecyclerViewAdapter_Search searchList_Recycler_Adapter;


    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.custom_toolbar);

        ActionBar action = getSupportActionBar();
        action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        Toolbar toolbar=(Toolbar)action.getCustomView().getParent();
        toolbar.setContentInsetsAbsolute(0, 0);
        toolbar.getContentInsetEnd();
        toolbar.setPadding(0, 0, 0, 0);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        drawerLayout = findViewById(R.id.my_drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);

        //list =  (ListView) findViewById(R.id.list_slidermenu);
        // pass the Open and Close toggle for the drawer layout listener
        // to toggle the button
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        // to make the Navigation drawer icon always appear on the action bar


        //runProgram();

        // data to populate the RecyclerView with

        RecyclerView recyclerView = findViewById(R.id.list_slidermenu);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        searchList_Recycler_Adapter = new RecyclerViewAdapter_Search(this, LoadData.vegListJSON);
        searchList_Recycler_Adapter.setClickListener(this);
        recyclerView.setAdapter(searchList_Recycler_Adapter);

        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView=(SearchView) findViewById(R.id.searchView);
        searchView.setFocusable(true);// searchView is null
        searchView.setFocusableInTouchMode(true);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        // set up the RecyclerView
       
     

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {


            return true;
        }
        return super.onOptionsItemSelected(item);
    }



好吧,我刚刚弄明白了。是“android:descendantFocusability="blocksDescendants" 这行把事情搞砸了。不知道我是怎么错过的。