前言
本篇比较简单,只是一个搜索组件简单的封装
实现
Java代码如下:
package com.floruit.cxy.view.widget;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import com.floruit.cxy.R;
import static android.content.Context.INPUT_METHOD_SERVICE;
/**
* 搜索组件
*/
public class SearchView extends LinearLayout {
private String hint = "请输入关键词";
private EditText et_input;
private ImageView iv_del;
private SearchChangeListener searchChangeListener;
public SearchView(Context context) {
super(context);
initView(context,null);
}
public SearchView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context,attrs);
}
public SearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context,attrs);
}
public void setSearchListener(SearchChangeListener searchListener){
this.searchChangeListener = searchListener;
}
public void setSearchText(String searchText){
if(TextUtils.isEmpty(searchText)) return;
et_input.setText(searchText);
et_input.setSelection(searchText.length());
}
private void initView(Context context, AttributeSet attrs) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_search_view,this,true);
et_input = view.findViewById(R.id.et_input);
iv_del = view.findViewById(R.id.iv_del);
setListener();
}
private void setListener() {
et_input.setOnKeyListener(new View.OnKeyListener() {//点击搜索按钮监听事件
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//是否是回车键
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//隐藏键盘
((InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(((Activity)getContext()).getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
et_input.clearFocus();
//搜索
if(searchChangeListener != null){
searchChangeListener.onSearchChanged(et_input.getText().toString().trim());
}
}
return false;
}
});
et_input.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(searchChangeListener != null){
searchChangeListener.onTextChanged(s.toString().trim());
}
}
@Override
public void afterTextChanged(Editable s) {
if (!TextUtils.isEmpty(s.toString().trim())) {
iv_del.setVisibility(View.VISIBLE);
} else {
iv_del.setVisibility(View.GONE);
}
}
});
iv_del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
et_input.setText("");
}
});
}
public interface SearchChangeListener{
void onSearchChanged(String keyWord);
void onTextChanged(String keyWord);
}
}
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/ll_search"
android:layout_width="match_parent"
android:layout_height="31dp"
android:layout_below="@+id/ll_simple_info"
android:background="@drawable/white_search_bg"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_vertical"
android:layout_marginStart="12dp"
android:layout_marginEnd="4dp"
android:src="@mipmap/search_icon"/>
<EditText
android:id="@+id/et_input"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="30dp"
android:layout_marginEnd="15dp"
android:layout_gravity="center_vertical"
android:hint="请输入关键词"
android:textColorHint="@color/search_hint"
android:textSize="14sp"
android:textColor="@color/black_2a"
android:background="@color/white"
android:gravity="start|center_vertical"
android:imeOptions="actionSearch"
android:singleLine="true"/>
<ImageView
android:id="@+id/iv_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@mipmap/search_close"
android:layout_marginEnd="@dimen/dp_10"
android:visibility="gone"/>
</LinearLayout>
其实很多组件的封装并不复杂,就是一个封装的思想,让项目中使用起来方便,可复用,业务逻辑清晰,易维护。
当然也有很多酷炫难度较高的自定义控件,这个是需要很多技术技巧的,慢慢学习和积累也是可以做到的。