【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. CentOS6.6从头到尾部署nginx与tomcat多实例

    前提条件: 1.需要一个全新的centos系统(本文中用到是centos6.6) 2.vmware虚拟机 3.vmware下安装centos系统,以NAT方式与宿主机相连 4.在centos系统中pi ...

  2. CUDA 显存操作:CUDA支持的C++11

    CUDA9的编译器和语言改进 使用CUDA 9,nvcc编译器增加了对C ++ 14的支持,其中包括新功能 通用的lambda表达式,其中使用auto关键字代替参数类型; auto lambda = ...

  3. DeepMind:所谓SACX学习范式

    机器人是否能应用于服务最终还是那两条腿值多少钱,而与人交互,能真正地做"服务"工作,还是看那两条胳膊怎么工作.大脑的智能化还是非常遥远的,还是先把感受器和效应器做好才是王道. 关于 ...

  4. Codeforces_765_D. Artsem and Saunders_(数学)

    D. Artsem and Saunders time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  5. HDU_2112_最短路

    题目链接:http://acm.hdu.edu.cn/status.php?user=l1526789512&pid=2112&status=5 HDU Today Time Limi ...

  6. Context Switches msdn

    Context Switches  https://msdn.microsoft.com/en-us/library/ms682105(VS.85).aspx The scheduler mainta ...

  7. PrintWriter与ServletOutputStream的区别之文件下载

    copy自:https://blog.csdn.net/weixin_37703598/article/details/803870611.out = response.getWriter(); re ...

  8. 参考KOA,5步手写一款粗糙的web框架

    我经常在网上看到类似于KOA VS express的文章,大家都在讨论哪一个好,哪一个更好.作为小白,我真心看不出他两who更胜一筹.我只知道,我只会跟着官方文档的start做一个DEMO,然后我就会 ...

  9. springBoot启动及发布

    1.在项目编辑器(IDEA)中启动 运行springBoot项目Application类中main方法,这两个按钮都可以.如下图: 当然还有我们配置的启动按钮,这是最常用的启动方式,不再赘述,如下图: ...

  10. linux性能优化cpu-02平均负载

    每次我们系统变慢时,我们通常做的第一件事就是top命令或者uptime命令,看一下系统的负载情况,比如下面: 我在命令行中输入uptime 22:15:51    表示当前系统时间 up 13 min ...