安卓自定义搜索控件

来源:互联网 发布:免费的数据恢复 编辑:程序博客网 时间:2024/06/05 10:22

功能

搜索功能在app中无处不在,每次写布局文件也是特别的繁琐,因此我们直接在这自定义一个搜索控件,该控件不依赖其他的xml或者属性资源。

效果预览

华为荣耀V9运行结果

代码预览

  • SearchView.java
public class SearchView extends EditText {  //搜索图标的颜色(引用的是系统的搜索图标,默认设置为红色)  private int searchImgColor = Color.RED;  //背景圆角,单位dp  private int cornerRadius = 10;  private SearchViewChangeListener listener;  public SearchView(Context context) {    super(context);    init();  }  public SearchView(Context context, @Nullable AttributeSet attrs) {    super(context, attrs);    init();  }  public SearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    init();  }  private void init() {    initBackground();    initSearchImg();    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) {      }      @Override      public void afterTextChanged(Editable s) {        if (listener != null) {          listener.onChange(SearchView.this,s.toString());        }      }    });  }  /**   * 初始化背景   */  private void initBackground() {    int backgroundColor = Color.GRAY;    Drawable drawable = getBackground();    if (drawable instanceof ColorDrawable) {      ColorDrawable colorDrawable = (ColorDrawable) drawable;      backgroundColor = colorDrawable.getColor();    }    GradientDrawable gradientDrawable = new GradientDrawable();    gradientDrawable.setCornerRadius(dp2px(cornerRadius));    gradientDrawable.setColor(backgroundColor);    setBackground(gradientDrawable);  }  /**   * 初始化搜索图标   */  private void initSearchImg() {    Drawable[] drawables = getCompoundDrawables();    if (drawables[0] == null) {      Drawable draw1 = this.getResources().getDrawable(android.R.drawable.ic_menu_search);      draw1.setColorFilter(searchImgColor, PorterDuff.Mode.SRC_IN);      draw1.setBounds(0, 0, draw1.getMinimumWidth(), draw1.getMinimumHeight());      setCompoundDrawables(draw1, null, null, null);    }  }  /**   * 设置搜索图标的颜色   *   * @param searchImgColor   */  public void setSearchImgColor(int searchImgColor) {    this.searchImgColor = searchImgColor;    postInvalidate();  }  public int dp2px(int dp) {    float density = getResources().getDisplayMetrics().density;    return (int) (dp * density + 0.5);  }  public void addSearchViewChangeListener(SearchViewChangeListener listener) {    this.listener = listener;  }  public interface SearchViewChangeListener {    void onChange(View view, String key);  }}
  • activity_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:fitsSystemWindows="true"  android:orientation="vertical"  >  <com.wxx.phone.widget.SearchView    android:id="@+id/searchView1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="默认的搜索"    android:padding="0dp"/>  <com.wxx.phone.widget.SearchView    android:id="@+id/searchView2"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="10dp"    android:background="#cccccc"    android:hint="xml布局文件带背景色的搜索"    android:padding="6dp"/>  <com.wxx.phone.widget.SearchView    android:id="@+id/searchView3"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="10dp"    android:background="#cccccc"    android:drawableLeft="@drawable/search"    android:hint="xml布局文件自定义搜索图标的"    android:padding="6dp"/></LinearLayout>
  • SearchActivity.java
public class SearchActivity extends Activity {  private final String TAG=TestActivity.class.getSimpleName();  private SearchView searchView1, searchView2, searchView3;  private SearchViewChangeListener listener;  @Override  protected void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_search);    searchView1 = (SearchView) findViewById(R.id.searchView1);    searchView2 = (SearchView) findViewById(R.id.searchView2);    searchView3 = (SearchView) findViewById(R.id.searchView3);    listener = new com.wxx.phone.widget.SearchView.SearchViewChangeListener() {      @Override      public void onChange(View view, String key) {        Log.e(TAG, view + "改变后的关键字>>>" + key);      }    };    searchView1.addSearchViewChangeListener(listener);    searchView2.addSearchViewChangeListener(listener);    searchView3.addSearchViewChangeListener(listener);  }}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 被qq好友拉黑了怎么办 网易邮箱字体变小了怎么办 邮箱注册不了全拼怎么办 家用电器接入两相火线后怎么办 手机充电板坏了怎么办 闪电宝商户乱跳怎么办 网易邮箱发错了怎么办 苹果手机邮件无法连接服务器怎么办 好记星平板电脑开不了机怎么办 邮箱里的文件过期了怎么办 小米手机邮件发不了怎么办 有人加我qq邮箱怎么办 文件太大发邮件太慢怎么办 爱又米发信息怎么办 手机上电子邮件己停止运行怎么办 苹果电子邮件密码忘了怎么办 玩游戏时电脑烫怎么办 qq邮箱独立密码忘记了怎么办 qq邮箱中转站容量不足怎么办 qq邮箱忘记密码了怎么办 qq邮箱超大附件过期怎么办 忘记qq邮箱独立密码怎么办 网易邮箱账号忘了怎么办 微信登录密码忘了怎么办 微信太久没登录登录不上怎么办 邮箱独立密码忘记了怎么办 苹果设置id没有邮箱怎么办 苹果手机设置id没有邮箱怎么办 邮箱的附件过期了怎么办 邮箱里的附件过期了怎么办 邮箱中附件过期了怎么办 扣扣邮箱附件过期怎么办 公司网页版邮箱进不去了怎么办 农行客户端密码忘记了怎么办 中国银行客户端密码忘记了怎么办 建行客户端登录密码忘记了怎么办 中国移动客户端密码忘记了怎么办 再歪一点授权码绑定怎么办 网易邮箱号忘了怎么办 忘记支付宝账号和密码怎么办 支付宝账号密码忘了怎么办