android系统控件系统控件Android开发

SearchView 笔记

2017-04-10  本文已影响345人  chauI

使用

<RelativeLayout
    android:id="@+id/search_edit_frame"
    style="@style/InfoLayoutStyle">

    <android.support.v7.widget.SearchView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
    <SearchView
        android:id="@+id/info_build_sv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"/>
</RelativeLayout>

要注意 android.widgetandroid.support.v7.widget 包里的用法是不太一样的。
特别是 android.support.v7.widget.SearchView 需要父布局 id 设为 search_edit_frame
否则会报错:

报错信息
// 设置搜索文本监听
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    // 当点击搜索按钮时触发该方法
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    // 当搜索内容改变时触发该方法
    @Override
    public boolean onQueryTextChange(String newText) {
        if (!TextUtils.isEmpty(newText)){
            
        }else{
            
        }
        return false;
    }
});
setIconifiedByDefault(boolean);//图标是否显示在搜索框内
setImeOptions(int);//输入法搜索选项字段,默认是搜索
setInputType(int);//输入类型
setMaxWidth(int);//最大宽度
setQueryHint(CharSequence);//提示字符串
android:iconifiedByDefault  
android:imeOptions  
android:inputType   
android:maxWidth    
android:queryHint

改变样式

SearchView 里集合了若干个 View,我们可以先获取到指定 View 的 id
根据 id 获取 View 的实例,以此改变样式

SearchView 源码
int imgId = mSearchView.getContext().getResources()
        .getIdentifier("android:id/search_mag_icon",null,null);
ImageView imageView = (ImageView)mSearchView.findViewById(imgId);
if (imageView != null){
    imageView.setImageResource(R.drawable.ic_location);
}
int plateId = mSearchView.getContext().getResources()
        .getIdentifier("android:id/search_plate",null,null);
View plate = mSearchView.findViewById(plateId);
plate.setBackgroundResource(R.color.white);

至于其他的 View 的 id 获取的方式,只要点进源码查看就可以了:

SearchView 源码

更多

实际上 SearchView 里有一个 SearchAutoComplete 完成搜索提示 / 历史记录的功能:

具体见于 Android SearchView的高级用法,解决关于SearchView的样式与控制问题

获取焦点的相关问题

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="60dp"
            android:focusable="true"
            android:focusableInTouchMode="true">

       <SearchView
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:inputType="textFilter"
                android:queryHint="输入IP"
                android:textColor="#ABABAB"
                android:textColorHint="#ABABAB" />

 </LinearLayout></span></span>

android:focusableandroid:focusableInTouchMode 设为 true
或者在 OnResume() 中设置:

mSearchView.setFocusable(true);
mSearchView.setFocusableInTouchMode(true);
//mSearchView.requestFocus();  //获取焦点
//如果软键盘已经出现则关闭软键盘
View view = getActivity().getWindow().peekDecorView();
if (view != null) {
    InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
上一篇 下一篇

猜你喜欢

热点阅读