在项目开发中,带删除button输入框也是人们经常常使用到的,该文章便介绍一下怎样创建一个带删除输入框。当中,须要解决的问题例如以下:

a)创建自己定义editText类

b)在自己定义editText中显示删除图片

c)依据输入框的输入情况显示或隐藏图片

d)点击删除图片文字消失,图片隐藏

e)依据输入框焦点失去和获得状态显示或隐藏图片

好了。问题明白了。開始实现功能:

a)创建一个名为MyClearEditText的class文件,并集成EditText,实现其构造方法:

	public MyClearEditText(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
} public MyClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
// TODO Auto-generated constructor stub
} public MyClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

ok,第一个问题攻克了,进入第二步。

b)在editText中。我们若想显示上下左右方向的图片,有着setCompoundDrawables或setCompoundDrawablesWithIntrinsicBounds方法,详细的话。能够去百度一下其差别,在这里,我使用的是setCompoundDrawablesWithIntrinsicBounds方法。代码例如以下:

	/**
* 初始化清除的图片
*/
private void initClearDrawable() {
draw = getCompoundDrawables()[2]; // 推断清除的图片是否为空
if (draw == null) {
draw = getResources().getDrawable(R.drawable.editdelete);
} // 为输入框设置图片
this.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);
}

思路为:先找到editText中右边的图片,若为null,则为其设置默认图片。然后再为输入框显示图片,便获得下图效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

c)须要获得输入框的情况。便要实现TextWatcher接口。

监听:

		this.addTextChangedListener(this);

须要实现的方法:

	public void onTextChanged(CharSequence text, int start, int lengthBefore,
int lengthAfter) {
// 推断输入框中是否有内容
if (text.length() > 0) {
this.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);
} else {
this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
}
} public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub } public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub }

d)怎么监听是点击到那个删除图片的呢,这是一个值得思考的问题,在这里,有一种解决方式。那便是触点监听。依据点击的位置来推断是否在图片所处位置的范围内:

	@Override
public boolean onTouchEvent(MotionEvent event) {
// 推断触碰是否结束
if (event.getAction() == MotionEvent.ACTION_UP) {
// 推断所触碰的位置是否为清除的button
if (event.getX() > (getWidth() - getTotalPaddingRight())
&& event.getX() < (getWidth() - getPaddingRight())) {
// 将editText里面的内容清除
setText("");
}
}
return super.onTouchEvent(event);
}

实现以上步骤后,大致的自己定义删除输入框功能便能够实现了,可是还是有些问题,假如有两个输入框。当向当中一个输入框输入文字后,点击另外一个输入框,上一个输入框还是会显示删除图片,解决方法例如以下:

e)既然是依据焦点的得失来推断,当然是实现焦点监听的方法:

	@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
// TODO Auto-generated method stub
super.onFocusChanged(focused, direction, previouslyFocusedRect);
// 推断焦点失去和得到时的操作
if (focused && !this.getText().toString().equals("")) {
this.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);
} else {
this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
}
}

ok。完整版的自己定义带删除输入框就全然实现了。为方便大家学习,下面为完整代码:

package com.xiaoyan.xiaoyanlibrary.common.widget.edittext;

import com.xiaoyan.xiaoyanlibrary.R;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText; /**
* 自己定义一个具有清除功能的editText
*
* @author xiejinxiong
*
*/
public class MyClearEditText extends EditText implements TextWatcher {
/** 储存清除的图片 */
private Drawable draw; public MyClearEditText(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
} public MyClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
// TODO Auto-generated constructor stub
} public MyClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); initClearDrawable();
this.addTextChangedListener(this);
} @Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
// TODO Auto-generated method stub
super.onFocusChanged(focused, direction, previouslyFocusedRect);
// 推断焦点失去和得到时的操作
if (focused && !this.getText().toString().equals("")) {
this.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);
} else {
this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
}
} /**
* 初始化清除的图片
*/
private void initClearDrawable() {
draw = getCompoundDrawables()[2]; // 推断清除的图片是否为空
if (draw == null) {
draw = getResources().getDrawable(R.drawable.editdelete);
} // 将输入框默认设置为没有清除的button
this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
} public void onTextChanged(CharSequence text, int start, int lengthBefore,
int lengthAfter) {
// 推断输入框中是否有内容
if (text.length() > 0) {
this.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);
} else {
this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
}
} public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub } public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub } @Override
public boolean onTouchEvent(MotionEvent event) {
// 推断触碰是否结束
if (event.getAction() == MotionEvent.ACTION_UP) {
// 推断所触碰的位置是否为清除的button
if (event.getX() > (getWidth() - getTotalPaddingRight())
&& event.getX() < (getWidth() - getPaddingRight())) {
// 将editText里面的内容清除
setText("");
}
}
return super.onTouchEvent(event);
} }

[Android]自己定义带删除输入框的更多相关文章

  1. 35.Android之带删除按钮EditText学习

    今天实现Android里自定义带删除功能的EditText,效果如下: 当输入内容时,EditText变为带有一个删除功能按钮的编辑框,如图: 实现代码很简单,直接上代码, 布局文件xml: < ...

  2. android自己定义TextView

    Android控件中的TextView控件仅仅有一个输入框.可是为了用于的操作方便我们应该实现一些功能: 1. 能够直接将内容删除的功能button 2. 可以记录用户曾经输入的数据,同一时候可以将数 ...

  3. android 自己定义控件

    Android自己定义View实现非常easy 继承View,重写构造函数.onDraw.(onMeasure)等函数. 假设自己定义的View须要有自己定义的属性.须要在values下建立attrs ...

  4. 网络编程之PC版与Android手机版带断点续传的多线程下载

    一.多线程下载         多线程下载就是抢占服务器资源         原理:服务器CPU 分配给每条线程的时间片相同,服务器带宽平均分配给每条线程,所以客户端开启的线程越多,就能抢占到更多的服 ...

  5. Android自己定义DataTimePicker(日期选择器)

    Android自己定义DataTimePicker(日期选择器)  笔者有一段时间没有发表关于Android的文章了,关于Android自己定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中.本篇 ...

  6. 【Android】自带Theme

    [Android]自带Theme android之uses-permission   在编写Android程序时经常会忘记添加权限,下面是网上收集的关于Android uses-permission的 ...

  7. Android SurfaceView实战 带你玩转flabby bird (下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43063331,本文出自:[张鸿洋的博客] 1.概述 在Android Surfa ...

  8. Android自己定义控件:进度条的四种实现方式

    前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...

  9. android自己定义开关控件

    近日在android项目要使用开关控件.可是android中自带的开关控件不太惬意,所以就打算通过自己定义View写一个开关控件 ios的开关控件当然就是我要仿照的目标. 先上图:   waterma ...

随机推荐

  1. 基于selenium爬取拉勾网职位信息

    1.selenium Selenium 本是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.而这一特性为爬虫开发提供了一个选择及方向,由于其本身依赖 ...

  2. jquery ajax示例

    $.ajax({ type: "POST",//方法类型 dataType: "json",//预期服务器返回的数据类型 url: "${ctx }/ ...

  3. 最近的一些JAVA基础知识

    1,关于判断两个值是否相等 equal 和==是有区别到 2,判断一个数组集合 List是否为空 这个不能用"==null或者equal"要用isEmpty() , 对于不等于加一 ...

  4. xtu字符串 D. 病毒侵袭

    D. 病毒侵袭 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class nam ...

  5. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) 圣诞之夜!

    A. Santa Claus and a Place in a Class 模拟题.(0:12) 题意:n列桌子,每列m张桌子,每张桌子一分为2,具体格式看题面描述.给出n,m及k.求编号为k的桌子在 ...

  6. 【Kubernetes】kube-dns 持续重启

    kuberbetes部署和启动正常,但是kube-dns持续重启 使用命令 kubectl get pods --all-namespaces 得到结果 从图中可以看出kube-dns-c7d8589 ...

  7. Spring Boot Reactive Streams

    1 响应式编程规范 目标:provide a standard for asynchronous stream processing with non-blocking backpressure ht ...

  8. 金明的预算方案(codevs 1155)

    题目描述 Description 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只 ...

  9. 【BZOJ1717&POJ3261】Milk Patterns(后缀数组,二分)

    题意:求字符串的可重叠的k次最长重复子串 n<=20000 a[i]<=1000000 思路:后缀数组+二分答案x,根据height分组,每组之间的height>=x 因为可以重叠, ...

  10. C#.net中当地址有中文时,图片无法显示解决方法

    原文发布时间为:2008-11-05 -- 来源于本人的百度文章 [由搬家工具导入] 搞了半天都无法正常显示图片, string path = Server.MapPath("." ...