搜索

1//main.xml中

 <com.bawei.day04code.view.MySearchView
        android:id="@+id/my_search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


2.//自定义view

public class MySearchView extends LinearLayout implements View.OnClickListener {
    private ImageView ivSearch;
    private EditText etContent;
    public MySearchView(Context context) {
        this(context, null);
    }
    public MySearchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public MySearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }
    /**
     * 初始化
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        inflate(context, R.layout.seach_view, this);
        findView();
        setOnClickListener();
    }
    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        ivSearch.setOnClickListener(this);
    }
    /**
     * 查找控件
     */
    private void findView() {
        ivSearch = findViewById(R.id.iv_search);
        etContent = findViewById(R.id.et_content);
    }
    /**
     * 返回搜索内容
     *
     * @return
     */
    public String getSearchConent() {
        return etContent.getText().toString();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_search:
                if (callback != null) {
                    callback.onSearch(getSearchConent());
                }
                break;
        }
    }
    private SearchViewCallback callback;
    public void setCallback(SearchViewCallback callback) {
        this.callback = callback;
    }
    public interface SearchViewCallback {
        void onSearch(String context);
    }
}

3.search.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/serch_bg"
    android:orientation="horizontal"
    android:padding="@dimen/search_padding">
    <ImageView
        android:id="@+id/iv_search"
        android:layout_width="@dimen/search_width"
        android:layout_height="@dimen/search_width"
        android:src="@drawable/a_4" />
    <EditText
        android:id="@+id/et_content"
        android:layout_width="0dp"
        android:layout_height="@dimen/search_width"
        android:layout_weight="1"
        android:background="@null" />
    <ImageView
        android:layout_width="@dimen/search_width"
        android:layout_height="@dimen/search_width"
        android:src="@drawable/root" />
</LinearLayout>

4,//mainactivity中

searchView = findViewById(R.id.my_search_view);
        searchView.setCallback(new MySearchView.SearchViewCallback() {
            @Override
            public void onSearch(String context) {
                Toast.makeText(MainActivity.this, context, Toast.LENGTH_SHORT).show();
            }
        });


猜你喜欢

转载自blog.csdn.net/weixin_42614228/article/details/81048037