有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在工具栏支持操作栏中设置搜索不起作用为什么?

我一直在尝试为我的应用添加/创建搜索功能!所以在通过安卓文档之后,就有了如何做到这一点的培训!我添加了它,因为它解释了,但它不工作,当我点击搜索图标,它没有反应,我甚至不能把任何文本进行搜索!我使用的是支持版本25

    <安卓.support.design.widget.CoordinatorLayout 
          xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
          xmlns:app="http://schemas.安卓.com/apk/res-auto"
          安卓:layout_width="match_parent"
          安卓:layout_height="match_parent">

              <include layout="@layout/toolbar"/>

              <安卓.support.v4.widget.NestedScrollView
              安卓:id="@+id/myScrollingContent"
              安卓:layout_width="match_parent"
              安卓:layout_height="match_parent">

                    <FrameLayout
                        安卓:id="@+id/contentContainer"
                        安卓:layout_width="match_parent"
                        安卓:layout_height="match_parent"
                        安卓:layout_above="@+id/bottom_bar" />

            </安卓.support.v4.widget.NestedScrollView>

            <com.roughike.bottombar.BottomBar
              安卓:id="@+id/bottom_bar"
              安卓:layout_width="match_parent"
              安卓:layout_height="60dp"
              安卓:layout_gravity="bottom"
              app:bb_tabXmlResource="@xml/bottombar_tabs_three"
              app:bb_behavior="shy"/>

     </安卓.support.design.widget.CoordinatorLayout>

共 (2) 个答案

  1. # 1 楼答案

    尝试使用查询侦听器。它检测在searchview中进行的任何输入

    SearchView simpleSearchView = (SearchView) findViewById(R.id.simpleSearchView);
                simpleSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        //searchedInput will be the text that was entered
                        String searchedInput = query;
                        //Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
                        return true;
                    }
    
                    @Override
                    public boolean onQueryTextChange(String newText) {
    
                        return false;
                    }
                });
    
  2. # 2 楼答案

    经过一番努力,我意识到这个错误是由于在协调器布局中包装视图造成的。所以工具栏的点击并没有被它后面添加的其他视图占据。所以我可以更改布局属性,但我决定改用相对布局

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:app="http://schemas.android.com/apk/res-auto">
    
                <include layout="@layout/toolbar"/>
    
                <FrameLayout
                    android:id="@+id/contentContainer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_above="@+id/bottom_bar" />
    
                <com.roughike.bottombar.BottomBar
                    android:id="@+id/bottom_bar"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_gravity="bottom"
                    android:layout_alignParentBottom="true"
                    app:bb_tabXmlResource="@xml/bottom_bar_tabs" />
    
        </RelativeLayout>