SearchView的使用

搜索功能是Android常见的功能之一,在v7包就有一个控件SearchView,用来实现搜索功能,而在普通widget包中也有一个SearchView,两者用处一样,只不过在使用方式上有一点区别,同时widget包中的SearchView只能在3.0(API 11)版本以上使用,而v7包中的SearchView能兼容到2.1(API 7).
今天介绍的就是v7包中的SearchView.

官方文档

使用前添加依赖:

compile 'com.android.support:appcompat-v7:25.3.1'

一、单独使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <android.support.v7.widget.SearchView
        android:id="@+id/searview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:iconifiedByDefault="false"
        app:queryHint="请输入搜索内容" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>
public class MainActivity extends AppCompatActivity {

    private SearchView mSearchView;
    private ListView mListView;
    private ArrayAdapter mAdapter;
    private String [] data = {"Java","kotlin","C","C++","C#","Python","PHP","JavaScript"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView = (ListView) findViewById(R.id.listview);
        mAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, data);
        mListView.setAdapter(mAdapter);
        mListView.setTextFilterEnabled(true);

        mSearchView = (SearchView) findViewById(R.id.searview);
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                mAdapter.getFilter().filter(newText);
                return false;
            }
        });
    }
}

这里为了书写简便就没有搭配RecyclerView了,而是搭配ListView使用,为SearchView设置了一个监听,当搜索框内容变化时,ListView动态过滤内容.

二、SearchView常用属性、方法

1、搜索图标

默认样式:搜索框默认是关闭的,左侧搜索图标在搜索框中,可以通过右侧叉叉关闭搜索框


默认样式
(1)  方法SearchView.setIconified(false);

搜索框默认是开启的,左侧搜索图标在搜索框中,可以通过右侧叉叉关闭搜索框
setIconified(false)
(2)  方法SearchView.setIconifiedByDefault(false);
或者属性app:iconifiedByDefault="false"

搜索框默认是开启的,左侧搜索图标在搜索框外
  右侧一开始没有叉叉,有输入内容后出现叉叉,叉叉只能清除搜索框内容,无法关闭搜索框
app:iconifiedByDefault="false"
(3)  修改搜索图标
 app:searchIcon=""  
2、搜索框中提示内容
app:queryHint=" "
或者方法:SearchView.setQueryHint(...)
3、关闭图标(就是那个叉叉)
app:closeIcon=" "
4、搜索框展开时显示提交按钮
SearchView.setSubmitButtonEnabled(true);
5、事件监听
    //搜索图标按钮的点击事件
    mSearchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "打开搜索框", Toast.LENGTH_SHORT).show();
        }
    });


    //搜索框内容变化监听
    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {//点击提交按钮时
            Toast.makeText(MainActivity.this, "Submit---提交", Toast.LENGTH_SHORT).show();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {//搜索框内容变化时
            if (!TextUtils.isEmpty(newText)) {
//              mListView.setFilterText(newText);
                mAdapter.getFilter().filter(newText);
            } else {
                mListView.clearTextFilter();
            }
            return true;
        }
    });

    //搜索框展开时点击叉叉按钮关闭搜索框的点击事件
    mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            Toast.makeText(MainActivity.this, "关闭搜索框", Toast.LENGTH_SHORT).show();
            return false;
        }
    });

三、搭配Toolbar使用

SearchView搭配Toolbar也是非常常见的一种使用方式.

1、方式一

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:titleTextColor="@android:color/white" />

</LinearLayout>

菜单文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_searchview"
        android:title="没什么鸟用的title"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />
</menu>

Activity中

public class MainActivity extends AppCompatActivity {

    private SearchView mSearchView;
    private Toolbar mToolbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
    }

    /**
     * 重写此方法显示Menu Item
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_menu, menu);
        MenuItem menuItem = menu.findItem(R.id.item_searchview);
        //通过MenuItem得到SearchView
        mSearchView = (SearchView) MenuItemCompat.getActionView(menuItem);
        return true;
    }
}

1)这个menu item跟普通item的差别在于使用了app:actionViewClass属性,app:actionViewClass="android.support.v7.widget.SearchView"表明使用了v7包下的SearchView.

2)注意:这里我们使用的是v7包下的SearchView,所以不要写成android:actionViewClass="android.widget.SearchView".

3)这里的title并没有什么鸟用,因为使用搜索功能时我们一般不会把它放进溢出菜单中,但是不写是会报错的.

4)如果在android:showAsAction属性中加上collapseActionView属性,那么MenuItem的icon属性必须就要加上了,不然会显示title文字的.
同时点开后的样式也不一样了,左侧会出现一个箭头,点击箭头退出搜索模式.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_searchview"
        android:title="没什么鸟用的title"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/icon_search"
        app:showAsAction="always|collapseActionView" />
</menu>
2、方式二

因为Toolbar本身是一个ViewGroup,可以添加子View,所以我们可以把SearchView当做子View添加到Toolbar上.
布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:titleTextColor="@android:color/white" >

        <android.support.v7.widget.SearchView
            android:id="@+id/searview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            app:queryHint="请输入搜索内容" />
    </android.support.v7.widget.Toolbar>>

</LinearLayout>

Activity中

public class MainActivity extends AppCompatActivity {

    private SearchView mSearchView;
    private Toolbar mToolbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        mSearchView = (SearchView) mToolbar.findViewById(R.id.searview);
        // do something
    }
}

四、修改SearchView样式

观看SearchView源码可以发现,SearchView本身布局是通过xml布局文件来实现的.

    public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        ......
        final LayoutInflater inflater = LayoutInflater.from(context);
        final int layoutResId = a.getResourceId(
                R.styleable.SearchView_layout, R.layout.abc_search_view); //☆  看这里
        inflater.inflate(layoutResId, this, true); 

        mSearchSrcTextView = (SearchAutoComplete) findViewById(R.id.search_src_text);
        mSearchSrcTextView.setSearchView(this);

        mSearchEditFrame = findViewById(R.id.search_edit_frame);
        mSearchPlate = findViewById(R.id.search_plate);
        mSubmitArea = findViewById(R.id.submit_area);
        mSearchButton = (ImageView) findViewById(R.id.search_button);
        mGoButton = (ImageView) findViewById(R.id.search_go_btn);
        mCloseButton = (ImageView) findViewById(R.id.search_close_btn);
        mVoiceButton = (ImageView) findViewById(R.id.search_voice_btn);
        mCollapsedIcon = (ImageView) findViewById(R.id.search_mag_icon);
        ......
    }

找到那个布局文件

<?xml version="1.0" encoding="utf-8"?>
<!--
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <!-- This is actually used for the badge icon *or* the badge label (or neither) -->
    <TextView
        android:id="@+id/search_badge"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:layout_marginBottom="2dip"
        android:drawablePadding="0dip"
        android:textAppearance="?attr/textAppearanceMedium"
        android:textColor="?attr/textColorPrimary"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/search_button"
        style="?attr/actionButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:focusable="true"
        android:contentDescription="@string/searchview_description_search" />

    <LinearLayout
        android:id="@+id/search_edit_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginStart="8dip"
        android:layout_marginEnd="8dip"
        android:orientation="horizontal"
        android:layoutDirection="locale">

        <ImageView
            android:id="@+id/search_mag_icon"
            android:layout_width="@dimen/dropdownitem_icon_width"
            android:layout_height="wrap_content"
            android:scaleType="centerInside"
            android:layout_marginStart="@dimen/dropdownitem_text_padding_left"
            android:layout_gravity="center_vertical"
            android:visibility="gone" />

        <!-- Inner layout contains the app icon, button(s) and EditText -->
        <LinearLayout
            android:id="@+id/search_plate"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:orientation="horizontal">

            <view class="android.widget.SearchView$SearchAutoComplete"
                android:id="@+id/search_src_text"
                android:layout_height="36dip"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:paddingStart="@dimen/dropdownitem_text_padding_left"
                android:paddingEnd="@dimen/dropdownitem_text_padding_right"
                android:singleLine="true"
                android:ellipsize="end"
                android:background="@null"
                android:inputType="text|textAutoComplete|textNoSuggestions"
                android:imeOptions="actionSearch"
                android:dropDownHeight="wrap_content"
                android:dropDownAnchor="@id/search_edit_frame"
                android:dropDownVerticalOffset="0dip"
                android:dropDownHorizontalOffset="0dip" />

            <ImageView
                android:id="@+id/search_close_btn"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:paddingStart="8dip"
                android:paddingEnd="8dip"
                android:layout_gravity="center_vertical"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:focusable="true"
                android:contentDescription="@string/searchview_description_clear" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/submit_area"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/search_go_btn"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:paddingStart="16dip"
                android:paddingEnd="16dip"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:visibility="gone"
                android:focusable="true"
                android:contentDescription="@string/searchview_description_submit" />

            <ImageView
                android:id="@+id/search_voice_btn"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:paddingStart="16dip"
                android:paddingEnd="16dip"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:visibility="gone"
                android:focusable="true"
                android:contentDescription="@string/searchview_description_voice" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

ok,既然是一个布局文件,而且每一个组件都有id,那么直接通过SearchView.findViewById()找到你想要改变的组件,然后设置你想要的样式不就可以了.

方式1 :代码修改
    //搜索框
    SearchView.SearchAutoComplete tv = (SearchView.SearchAutoComplete) mSearchView.findViewById(R.id.search_src_text);
    tv.setTextColor(Color.WHITE);
    tv.setHintTextColor(Color.WHITE);
    //搜索按钮
    ImageView ivSearch1 = (ImageView) mSearchView.findViewById(R.id.search_button);
    ivSearch1.setImageResource(R.drawable.a);
    //关闭按钮
    ImageView ivClose = (ImageView) mSearchView.findViewById(R.id.search_close_btn);
    ivClose.setImageResource(R.drawable.b); 

更多的样式你可以自己去设置,这里就不一一设置了.

方式2:style修改

原生的SearchView样式像下面这样:

    <style name="Base.Widget.AppCompat.SearchView" parent="android:Widget">
        <item name="layout">@layout/abc_search_view</item>
        <item name="queryBackground">@drawable/abc_textfield_search_material</item>
        <item name="submitBackground">@drawable/abc_textfield_search_material</item>
        <item name="closeIcon">@drawable/abc_ic_clear_material</item>
        <item name="searchIcon">@drawable/abc_ic_search_api_material</item>
        <item name="searchHintIcon">@drawable/abc_ic_search_api_material</item>
        <item name="goIcon">@drawable/abc_ic_go_search_api_material</item>
        <item name="voiceIcon">@drawable/abc_ic_voice_search_api_material</item>
        <item name="commitIcon">@drawable/abc_ic_commit_search_api_mtrl_alpha</item>
        <item name="suggestionRowLayout">@layout/abc_search_dropdown_item_icons_2line</item>
    </style>

我们可以在上面做一些修改:

    <style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
        <item name="android:layout">@layout/abc_search_view</item>
        <item name="queryBackground">@drawable/abc_textfield_search_material</item>
        <item name="submitBackground">@drawable/abc_textfield_search_material</item>
        <item name="closeIcon">@drawable/abc_ic_clear_material</item>
        <item name="searchIcon">@drawable/abc_ic_search_api_material</item>
        <item name="goIcon">@drawable/abc_ic_go_search_api_material</item>
        <item name="voiceIcon">@drawable/abc_ic_voice_search_api_material</item>
        <item name="commitIcon">@drawable/abc_ic_commit_search_api_mtrl_alpha</item>
        <item name="suggestionRowLayout">@layout/abc_search_dropdown_item_icons_2line</item>

        <item name="defaultQueryHint">请输入搜索内容</item>
        <item name="searchHintIcon">@drawable/icon_search</item>
    </style>

然后引用该样式即可,想要全局修改直接放在主题style中:

<item name="searchViewStyle">@style/MySearchViewStyle</item>

只想修改某一个的话给那个SearchView引用即可

style="@style/MySearchViewStyle"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,568评论 25 707
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,085评论 2 44
  • 一年一度的教师节又到了。 而最好的礼物,莫过于对老师的尊重。 孙文光是安徽师范大学的教授。孙教授夫妻俩和...
    拂去心尘阅读 1,459评论 2 2
  • 有些图片,能让你感觉心灵杯净化一般,慢慢欣赏
    黑白律动阅读 2,968评论 0 0
  • 处在分岔路口,一条是自己喜欢的,一条是要变成自己不喜欢的那种人;而这种人又能适应社会的发展,他们圆滑,功利,但却能...
    静晴微笑阅读 177评论 0 0