自定义Android SearchView

自定义Android SearchView

Android自带的SearchView很难满足项目的需要,所以就自定义了一个SearchView来使用;对外调用的方法只有2个,分别是setSearchHint(String arg)和setOnQueryTextListener(SearchView.OnQueryTextListener listener);
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:id="@+id/search_layout"
              android:background="@drawable/shape"
              android:layout_height="match_parent"
              android:orientation="horizontal">
    <ImageView
        android:id="@+id/search_button"
        android:layout_width="30dp"
        android:layout_marginLeft="10dp"
        android:layout_height="30dp"
        android:layout_gravity="center_vertical"
        android:background="@mipmap/search"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/search_text"
            android:layout_width="0dp"
            android:layout_weight="18"
            android:focusable="true"
            android:maxLines="1"
            android:imeOptions="actionSearch"
            android:background="@null"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:hint="请输入查询内容"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/search_close"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:visibility="visible"
                android:layout_gravity="right|center_vertical"
                android:background="@mipmap/delete"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

其中的shape文件见shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >
    <corners android:radius="1dp" />
    <!-- 这是半透明,还可以设置全透明,那就是白色边框的效果了 -->
    <solid android:color="#FFFFFF" />
    <stroke
        android:dashGap="0dp"
        android:width="1dp"
        android:color="#FFD9D1D4" />
</shape>

剩下的就是实现的代码块了,详细见HeyhaSearchView.java文件:

public class HeyhaSearchView extends LinearLayout implements View.OnClickListener {
    private static final String TAG = HeyhaSearchView.class.getName();

    private EditText searchText;
    private ImageView searchClose;
    private boolean isEditting = false;
    private SearchView.OnQueryTextListener mOnQueryChangeListener;
    protected TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            Log.i(TAG, "beforeTextChanged:" + charSequence.toString());
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            Log.i(TAG, "onTextChanged:" + charSequence.toString());
            updateCloseButton();
            if (mOnQueryChangeListener != null) {
                mOnQueryChangeListener.onQueryTextChange(searchText.getText().toString().trim());
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
            Log.i(TAG, "afterTextChanged:");
        }
    };
    private OnFocusChangeListener onFocusChangeListener = new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            isEditting = b;
            if (isEditting) {
                searchClose.setVisibility(VISIBLE);
            } else {
                searchClose.setVisibility(GONE);
            }
        }
    };

    public HeyhaSearchView(Context context) {
        this(context, null);
    }

    public HeyhaSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.search_view, this);
        searchClose = (ImageView) view.findViewById(R.id.search_close);
        searchText = (EditText) view.findViewById(R.id.search_text);
        searchText.addTextChangedListener(textWatcher);
        searchClose.setOnClickListener(this);
        searchText.setOnEditorActionListener(mOnEditorActionListener);
        searchText.setOnFocusChangeListener(onFocusChangeListener);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.search_close) {
            searchText.setText("");
            //此时会执行onTextChanged(),所以不需要下面的方法
//            mOnQueryChangeListener.onQueryTextSubmit("");
        }
    }

    //点击键盘上搜索按钮时执行查询操作,查询的内容由onQueryTextSubmit()的参数提供
    private final TextView.OnEditorActionListener mOnEditorActionListener = new TextView
            .OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            Log.i(TAG, "oneditoraction:" + v.getText().toString());
            mOnQueryChangeListener.onQueryTextSubmit(searchText.getText().toString().trim());
            return true;
        }
    };

    /**
     * 提供给外部进行查询的接口,使用该查询框必须实现该方法
     *
     * @param listener
     */
    public void setOnQueryTextListener(SearchView.OnQueryTextListener listener) {
        mOnQueryChangeListener = listener;
    }

    /**
     * 提供给外部设置hint
     *
     * @param arg
     */
    public void setSearchHint(String arg) {
        searchText.setHint(arg);
    }

    /**
     * 设置输入框清除按钮的显示
     */
    private void updateCloseButton() {
        final boolean hasText = !TextUtils.isEmpty(searchText.getText().toString().trim());
        final boolean showClose = hasText;
        searchClose.setVisibility(showClose ? VISIBLE : INVISIBLE);
    }
}

//注释比较详细,所以在此就不再做解释了,只能对文本进行搜索,不能输入语音。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值