【Android自定义View实战】之自定义超简单SearchView搜索框
2014-07-18 16:15
615 查看
【Android自定义View实战】之自定义超简单SearchView搜索框
这篇文章是对之前文章的翻新,至于为什么我要重新修改这篇文章?原因如下1.有人举报我抄袭,原文链接:http://www.it165.net/pro/html/201407/17779.html。呵呵...................................................................请大家仔细看看,那个图片水印。到底是谁抄袭谁呢?2.之前的那篇文章写得非常随意,今天先到来封装一个自定义View,使用起来更方便。在Android开发中我们经常会用到搜索框,而系统提供的又不尽完美。所以自定义一个比较简单的SearchView。代码非常简单,高手请略过。
效果图
实现代码
1.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginRight="12dp" android:layout_marginTop="5dp" android:background="@drawable/search_bg" android:orientation="horizontal"> <Button android:layout_width="20dp" android:layout_height="20dp" android:layout_gravity="right|center_vertical" android:layout_margin="10dp" android:background="@mipmap/search" /> <!-- 输入的搜索信息 --> <EditText android:id="@+id/et_search" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_marginLeft="10dp" android:layout_weight="4" android:background="@null" android:gravity="center_vertical" android:hint="输入内容进行搜索" android:imeOptions="actionSearch" android:singleLine="true" android:textColor="#0e0e0e" android:textColorHint="#b0c6ce" android:textSize="17sp" /> <Button android:id="@+id/bt_clear" android:layout_width="25dp" android:layout_height="25dp" android:layout_gravity="right|center_vertical" android:layout_margin="10dp" android:background="@mipmap/delete" /> </LinearLayout> </LinearLayout>
2.java代码
package cn.bluemobi.dylan.searchview; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; /** * Android自定义SearchView * Created by yuandl */ public class SearchView extends LinearLayout implements TextWatcher, View.OnClickListener { /** * 输入框 */ private EditText et_search; /** * 输入框后面的那个清除按钮 */ private Button bt_clear; public SearchView(Context context, AttributeSet attrs) { super(context, attrs); /**加载布局文件*/ LayoutInflater.from(context).inflate(R.layout.pub_searchview, this, true); /***找出控件*/ et_search = (EditText) findViewById(R.id.et_search); bt_clear = (Button) findViewById(R.id.bt_clear); bt_clear.setVisibility(GONE); et_search.addTextChangedListener(this); bt_clear.setOnClickListener(this); } @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } /**** * 对用户输入文字的监听 * * @param editable */ @Override public void afterTextChanged(Editable editable) { /**获取输入文字**/ String input = et_search.getText().toString().trim(); if (input.isEmpty()) { bt_clear.setVisibility(GONE); } else { bt_clear.setVisibility(VISIBLE); } } @Override public void onClick(View view) { et_search.setText(""); } }
3.具体功能的实现以上只是对界面的写法,下面是对搜索记录功能实现: 【玩转SQLite系列】(六)SQLite数据库应用案例实现历史搜索记录
GitHub
2.java代码
相关文章推荐
- 【Android自己定义View实战】之自己定义超简单SearchView搜索框
- Android自定义简单的View
- 【Android】 自定义ViewGroup 实战篇 -> 实现FlowLayout
- Android学习之路--简单的ViewPager(二 自定义ViewPager)
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
- 【Android实战】记录自学自定义GifView过程,能同时支持gif和其他图片!【实用篇】
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
- android自定义View之(二)------简单进度条显示样例篇
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
- android:滑动挂断自定义View的简单实现
- Android实战简易教程-第三十三枪(自定义View实现控件晃动提示效果)
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
- Android 自定义View、ViewGroup 实战训练之CustomFrameDialog
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
- Android自定义简单的View
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
- Android开发实战2----圆点导航指示器(使用自定义View实现)
- Android 手把手教您自定义ViewGroup(二) 实战篇 -> 实现FlowLayout
- Android实战简易教程-第三十二枪(自定义View登录注册界面EditText-实现一键清空)
- 【Android实战】记录自学自定义GifView过程,详解属性那些事!【学习篇】