【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. VMWare 支持的网络连接类型 (VMWare Virtual Network Connection Types)

  2. 牛客多校Round 6

    Solved:3 rank:156 J. Heritage of skywalker 学习一下nth_element 可以o (n)的找出前多少大的元素 #include <bits/stdc+ ...

  3. ltp-ddt qspi_mtd_dd_rw error can't read superblock on /dev/mtdblock0

    can't read superblock on /dev/mtdblock0 1.fsck /dev/xxx 2.e2fsck -b 8193 <device> e2fsck -b 32 ...

  4. JAVA学习笔记16——线程生命周期

    当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态,在线程的生命周期中,它要经过新建(New).就绪(Runnable).运行(Running).阻塞(Blocking)和 ...

  5. post请求重定向到get请求问题

    springMVC默认重定向是get请求,我在方法注解中没有指定method是post还是get请求,这样就可以接收到post重定向来的请求,也可以接收到页面传来的get请求,如果要传参,可以使用mo ...

  6. Android开发技巧一--weight属性实现视图的居中(半)显示

    面试时,一位面试官问到:“如果我想讲按钮居中显示,并且占据其父视图宽度的一半,应该怎么做到呢?”即实现这种效果: 我们使用weightSum属性和layout_weight属性实现这一要求: < ...

  7. Plan & Future

    以下是OI省选前的数据结构与算法整理,可能还不是很全面.但是已经是全网相对比较全面的了.所有标记为“基础”“进阶”“中级”“提高”的知识为近些年来NOIp考察的内容,需重点掌握. 所有“高级”部分为N ...

  8. POJ3616 Milking Time【dp】

    Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti ...

  9. MyBatis 创建核心配置文件和 SQL 映射文件

    Mybatis 的两个配置文件(mybatis-config.xml  和 xxxMapper.xml)都为 xml 类型,因此在 eclipse 中创建 xml 文件命名为相应的 mybatis-c ...

  10. stall and flow separation on airfoil or blade

    stall stall and flow separation Table of Contents 1. Stall and flow separation 1.1. Separation of Bo ...