安卓自定义搜索控件
来源:互联网 发布:免费的数据恢复 编辑:程序博客网 时间:2024/06/05 10:22
功能
搜索功能在app中无处不在,每次写布局文件也是特别的繁琐,因此我们直接在这自定义一个搜索控件,该控件不依赖其他的xml或者属性资源。
效果预览
代码预览
- 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); }}
阅读全文
0 0
- 安卓自定义搜索控件
- 安卓动画搜索框自定义控件
- 自定义安卓控件
- 安卓----自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件,自定义控件属性
- 安卓自定义日历控件
- 安卓自定义日历控件
- 安卓自定义控件 - 进度条
- 安卓时间控件自定义
- 安卓入门---自定义控件
- 安卓自定义组合控件
- svn的服务器地址改变后如何修改
- 技术分享连载(四十二)
- HIS-门急诊模块之系统集成工作摘要
- 几款在线脑图制作工具
- Eclipse 自定义本地maven仓库位置
- 安卓自定义搜索控件
- 几款.Net加密/加壳工具的比较
- linux下dlib库的安装
- Fiddler抓包5-接口测试(Composer)
- MATLAB Floor 用法
- 用python验证Android签名机制
- centos7-codis安装部署,解决redis分布式的方案
- Spring IOC原理源码解析(@Autowired原理详解 :标识属性与方法)(二 )
- cannot lock ref问题的解决