欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

小记:searchview

程序员文章站 2022-03-11 22:39:02
...

searchview

先导包

  implementation 'com.miguelcatalan:materialsearchview:1.4.0'

然后再xml中去写一个控件

<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <androidx.appcompat.widget.Toolbar
       android:id="@+id/tooolbar"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
       android:background="@color/colorPrimary" />
    <com.miguelcatalan.materialsearchview.MaterialSearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</FrameLayout>

去主函数里去实例化

  Toolbar toolbar=findViewById(R.id.tooolbar);
            toolbar.setTitle("nmsl");
            setSupportActionBar(toolbar);

        searchView = (MaterialSearchView) findViewById(R.id.search_view);

基本控件实例化
设置点击监听

  searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    //Do some magic
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    //Do some magic
                    return false;
                }
            });

            searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
                @Override
                public void onSearchViewShown() {
                    //Do some magic
                    //搜索框展开
                    Toast.makeText(MainActivity.this, "展开", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onSearchViewClosed() {
                    //Do some magic
                    //搜索框关闭
                    Toast.makeText(MainActivity.this, "关闭", Toast.LENGTH_SHORT).show();
                }
            });
            searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {






                }
            });

里面需要用到菜单
所以新建菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_action_search"
        android:orderInCategory="100"
        android:title="@string/abc_search_hint"
        app:showAsAction="always" />
</menu>

再菜单里面去把控件设置好,才能具有点击效果

  @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu,menu);
            MenuItem item = menu.findItem(R.id.action_search);
            searchView.setMenuItem(item);
            searchView.setHint("sad");


           return true ;
        }

包装一下

     }
    @Override
    public void onBackPressed() {
        if (searchView.isSearchOpen()) {
            searchView.closeSearch();
        } else {
            super.onBackPressed();
        }
    }
相关标签: 小记