【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代码

【Android自己定义View实战】之自己定义超简单SearchView搜索框的更多相关文章

  1. (转)Android SearchView 搜索框

    如果对这个效果感觉不错, 请往下看. 背景: 天气预报app, 本地数据库存储70个大中城市的基本信息, 根据用户输入的或通过搜索框选取的城市, 点击查询按钮后, 异步请求国家气象局数据, 得到返回的 ...

  2. Android之不须要自己定义View(ViewfindView.java)最简单的二维码扫描

    不废话,先爆照 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  3. 自己定义 View 基础和原理

    课程背景: 在 Android 提供的系统控件不能满足需求的情况下,往往须要自己开发自己定义 View 来满足需求,可是该怎样下手呢.本课程将带你进入自己定义 View 的开发过程,来了解它的一些原理 ...

  4. Path类的最全面具体解释 - 自己定义View应用系列

    前言 自己定义View是Android开发人员必须了解的基础:而Path类的使用在自己定义View绘制中发挥着很关键的数据 网上有大量关于自己定义View中Path类的文章.但存在一些问题:内容不全. ...

  5. Android开发 ---代码创建选项菜单、隐藏菜单项、菜单的生命周期,菜单按钮图标设置、搜索框、xml中设置子菜单

    1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> < ...

  6. Android混淆、反编译以及反破解的简单回顾

    =========================================================================虽然反编译很简单,也没下面说的那么复杂,不过还是转了过 ...

  7. Android 系统搜索框(有浏览记录)

    实现Android 系统搜索框(有浏览记录),先看下效果: 一.配置搜索描述文件  要在res中的xml文件加创建sreachable.xml,内容如下: <?xml version=" ...

  8. Android 它们的定义View (一)

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 非常Android入门程序员AndroidView.可能都是比較恐 ...

  9. Android 它们的定义View它BounceProgressBar

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/41246247 [Rocko's blog] 之前几天下载了非常久没用了的桌面版酷狗来用用的时候 ...

随机推荐

  1. 小知识~清除系统盘的Hiberfil.sys

    Hiberfil.sys这个文件是系统休眠用的,时间长了你可能会占用几个G的磁盘空间,有时我们并不需要它,而又无法直接删除,这时,你可以使用CMD命令来关闭这个功能,关闭后,这个文件自动被删除. 1 ...

  2. 执行join_paired_ends.py报错Cannot find fastq-join

    通过 conda 安装 qiime 1后,在执行join_paired_ends.py时报错: burrito.util.ApplicationNotFoundError: Cannot find f ...

  3. Vue课程思维导图

  4. Mybatis学习总结三(动态SQL)

    通过mybatis提供的各种标签方法实现动态拼接sql. 一.if 和 where <select id="findUserList" parameterType=" ...

  5. 【原】SMTP发送邮件

    1.下载class.phpmailer.php和class.smtp.php至公共库 2.编写发邮件的公共函数 function sendMail($param) { $config = C('THI ...

  6. oracle 内连接 左外连接 右外连接的用法,(+)符号用法

    1. 内连接很简单 select A.*, B.* from A,B where A.id = B.id select A.*, B.* from A inner join B on A.id = B ...

  7. 60s倒计时

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 「 Luogu P2285 」打鼹鼠

    解题思路 第一眼看上去觉得要设计一个三维的 DP,$dp[i][j][k]$ 表示在 $(i,j)$ 这个位置上 $k$ 时刻能够打死的最多的鼹鼠. 但是被数据范围卡死.完全开不开数组啊. 然后注意到 ...

  9. Navicat for MySQL(Ubuntu)过期解决方法

    推荐购买正版软件,尊重版权  [官网在这里] Navicat for MySQL(Ubuntu系统)免费版试用过期解决方法: Step1. 直接删除 /home目录下的  .navicat文件夹(64 ...

  10. zabbix部署-版本3.2.6

    172.18.237.14:一台主机上安装LAMP环境以及zabbix_server.zabbix_agentd 一.安装zibbix-server 1.环境要求 yum install mysql- ...