android自定义SearchView

编写布局文件

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

search_layout.xml


<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


<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来进行搜索操作和取消软键盘显示。

你可能感兴趣的:(Android,Android自定义控件)