【Android自己定义View实战】之自己定义超简单SearchView搜索框

这篇文章是对之前文章的翻新,至于为什么我要又一次改动这篇文章?原因例如以下
1.有人举报我抄袭,原文链接:http://www.it165.net/pro/html/201407/17779.html

呵呵...................................................................请大家细致看看,那个图片水印。

究竟是谁抄袭谁呢?

2.之前的那篇文章写得很任意。今天先到来封装一个自己定义View。使用起来更方便。

在Android开发中我们常常会用到搜索框。而系统提供的又不尽完美。

所以自己定义一个比較简单的SearchView。

代码很easy,高手请略过。

效果图

raw=true" alt="" />

实现代码
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;
/**
* 输入框后面的那个清除button
*/
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数据库应用案例实现历史搜索记录

2.java代码

最新文章

  1. 初识linux端c++程序开发
  2. CardLayout布局练习(小的图片浏览器)
  3. org.hibernate.HibernateException: A collection with cascade=&quot;all-delete-orphan&quot; was no longer referenced by the owning entity instance:
  4. GLSL实现Simple Displace Mapping 水仿真流体绘制 【转】
  5. Best practice for Invoke other scripts or exe in PowerShell
  6. java_部署jar
  7. JavaScript嗅探执行神器-sniffer.js,你值得拥有!
  8. Day-11: IO编程
  9. 201521123080《Java程序设计》第9周学习总结
  10. 再起航,我的学习笔记之JavaScript设计模式23(中介者模式)
  11. 要搞刷机!从它的尸体上踏过去!钢板云路由!WPR003N复活!成功启动OPENWRT
  12. echarts中legend如何换行
  13. HTML之超链接
  14. vue 开发微信小程序
  15. centos7下编译安装nginx-1.16.0
  16. flask --- 02. 路由, 初始化配置,蓝图
  17. 挂载银行前置机Ukey到windows server2012虚拟机的操作记录
  18. 2.0 vue内置指令与自定义指令
  19. phpmailer使用qq邮箱、163邮箱成功发送邮件实例代码
  20. Job for apache2.service failed because the control process exited with error code. See &quot;systemctl status apache2.service&quot; and &quot;journalctl -xe&quot; for details.

热门文章

  1. Linux 学习(三)
  2. 接口与抽象类的区别-Java
  3. form表单清空、重置
  4. Deployd的使用
  5. break和continue在多重循环中使用
  6. 牛客多校Round 3
  7. xilinx vivado 百度云分享 vivado2019.1 2018.3 2017.4
  8. 右键快捷打开Git Bash here失败
  9. 洛谷——P1349 广义斐波那契数列(矩阵加速)
  10. OS X中微信双开