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

SearchView

程序员文章站 2024-03-07 20:14:57
...

布局:

XML布局:
    <android.support.v7.widget.SearchView
    android:id="@+id/search_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:imeOptions="actionSearch"
    app:iconifiedByDefault="false"
    app:goIcon="@android:drawable/ic_search_category_default"
    app:queryHint="@string/hint"
    app:searchHintIcon="@android:drawable/sym_call_missed"
    app:searchIcon="@android:drawable/sym_action_email" />

        iconifiedByDefault设置为false,默认展开
        goIcon点击搜索按钮
        queryHint搜索提示
        searchHintIcon提示图标
        searchIcon收缩的图标
    获取实例:
        mSearchView = (SearchView) findViewById(R.id.search_view);

联合toolbar
     <item
    android:id="@+id/search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="menu搜索"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="collapseActionView|ifRoom" />
    获取实例
    MenuItem searchMenuItem = menu.findItem(R.id.search);
    //使用v7的时候,需要使用MenuItemCompat
    SearchView searchView =
            (SearchView) MenuItemCompat.getActionView(searchMenuItem)

常用功能:

mSearchView.setIconifiedByDefault(false);  //设置false,SearchView会一直处于展开状态
mSearchView.setIconified(false);  //设置为false,SearchView会展开,反之会缩成1个Icon
mSearchView.setQueryHint(getString(R.string.hint));  //设置提示文字
mSearchView.setSubmitButtonEnabled(true);

1、监听器

mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener())

2、获取实例

通过反射:

try {
        Field field = mSearchView.getClass().getDeclaredField("mSearchSrcTextView");
        field.setAccessible(true);
        //通过反射拿到SearchView里面的SearchAutoComplete组件,输入框里的文字
        mSearchSrcTextView = (SearchView.SearchAutoComplete) field.get(mSearchView);
        //同样的
                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);
                mSearchPlate = findViewById(R.id.search_plate);//下划线
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

通过findViewById

    mSearchSrcTextView = (SearchView.SearchAutoComplete)
            mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

3、搜索实例

    int id_wight = searchView.getResources().getIdentifier("android:id/search_src_text", null, null);
    int id_v7 = R.id.search_src_text;
    AutoCompleteTextView completeText = (AutoCompleteTextView) searchView
            .findViewById(id_v7) ;
    completeText.setThreshold(0);//代表输入长度为0即显示提示列表,往后以此类推。
    completeText.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list));//数据源
    completeText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            searchView.setQuery(testStrings[position],true);//ture表示提交该字符串,为false为不提交。
        }
    });

4、取消默认自动获取焦点

    设置父控件的属性
    android:focusable="true"
    android:focusableInTouchMode="true"

    //如果软键盘已经出现则关闭软键盘
    View view = getActivity().getWindow().peekDecorView();
    if (view != null) {
     InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
              .getSystemService(Context.INPUT_METHOD_SERVICE);
     inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
              }