android自定义SearchView

编写布局文件

本质就是LinearLayout包含着一个EditTextView和ImageView,ImageView显示的是一个清楚按钮,用来清除文本,然后把EditTextView的背景设置为null,再设置左边的搜索图片,给LinearLayout设置圆角背景,看起来就像EditTextView和ImageView是一个整体

search_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:background="@drawable/search_bg"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/et_search"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:imeOptions="actionSearch"
            android:textColor="#0e0e0e"
            android:textSize="18sp"
            android:singleLine="true"
            android:hint="搜索"
            android:textColorHint="#b0c6ce"
            android:gravity="center_vertical"
            android:drawableLeft="@drawable/search_left"
            android:background="@null"/>


        <ImageView
            android:id="@+id/btn_clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:clickable="true"
            android:background="@drawable/clear" />

    </LinearLayout>

</LinearLayout>

search_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 圆角 -->
    <corners android:radius="5dp"/>
    <solid android:color="#ffffff"/>
    <stroke android:color="#d3dde6" android:width="1px"/>
</shape>

android:imeOptions=”actionSearch”这一行作用是弹出的键盘enter键是搜索字样。

自定义SearchView

public class SearchView extends LinearLayout{

    public interface OnSearchListener {
        public void onSearch(String text);
    }

    private OnSearchListener listener;

    @BindView(R.id.et_search)
    public EditText editText;

    @BindView(R.id.btn_clear)
    public ImageView clearButton;

    private Context context;

    public SearchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.search_layout, this);
        ButterKnife.bind(this);
        initViews();
    }

    public void setListener(OnSearchListener listener) {
        this.listener = listener;
    }

    private void initViews() {
        clearButton.setOnClickListener(v -> {
            editText.setText("");
            clearButton.setVisibility(GONE);
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!"".equals(s.toString())) {
                    clearButton.setVisibility(VISIBLE);
                }else {
                    clearButton.setVisibility(GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if (listener != null) {
                        listener.onSearch(textView.getText().toString());
                    }
                    hideSoftInput(context);
                }
                return true;
            }
        });
    }

    public String getInputText() {
        return editText.getText().toString();
    }

    public void hideSoftInput(Context context) {
        View view = ((Activity) context).getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}

由于只有ViewGroup可以直接inflate,所以继承LinearLayout。设置文本监听,如果没有文本则不现实clear的ImageView,反之显示。点击clear清除文本,同时监听键盘的enter来进行搜索操作和取消软键盘显示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值