转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685

自定义EditText带删除小图标,

实现的功能:

   点击删除小图标,删除当前输入框中所有内容

   删除图标默认不显示,当输入框获得焦点后显示,

实现的操作:

在Edittext的DrawableRight中添加一张删除图标,作为删除功能的小图标

因为Edittext不能为图片设置点击监听事件,因此我们需要自定义Edittext在onTouchEvent方法中模拟按钮点击的操作

当我们触摸抬起(就是ACTION_UP的时候)的范围  大于输入框左侧到清除图标左侧的距离,小与输入框左侧到清除图片右侧的距离,我们则认为是点击清除图片,

xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:fitsSystemWindows="true"
  6. android:background="#B0E0E6"
  7. >
  8.  
  9. <TextView
  10. android:id="@+id/activity_login_ttitle"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:text="登录账号"
  14. android:textColor="#ffffff"
  15. android:background="#6699ff"
  16. android:padding="15dp"
  17. android:textSize="25sp"
  18. android:gravity="center"/>
  19. <ImageView
  20. android:id="@+id/activity_login_icon"
  21. android:layout_width="150dp"
  22. android:layout_height="150dp"
  23. android:layout_centerHorizontal="true"
  24. android:layout_below="@id/activity_login_ttitle"
  25. android:layout_margin="15dp"/>
  26. <LinearLayout
  27. android:id="@+id/activity_login_ll_layout2"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:orientation="horizontal"
  31. android:gravity="center_vertical"
  32. android:layout_below="@id/activity_login_icon"
  33. android:padding="15dp"
  34. >
  35. <TextView
  36. android:layout_width="wrap_content"
  37. android:layout_height="match_parent"
  38. android:textSize="20sp"
  39. android:gravity="center_vertical"
  40. android:textColor="#000000"
  41. android:text="账 号:"/>
  42. <com.my.myapp.customeView.CustomeEditext
  43. android:id="@+id/activity_login_et_user"
  44. android:layout_width="match_parent"
  45. android:layout_height="match_parent"
  46. android:textSize="18sp"
  47. android:singleLine="true"
  48. android:background="@drawable/edittext_bg_selector"
  49. android:drawableRight="@drawable/delete_selector"
  50. android:hint="请输入账号"/>
  51.  
  52. </LinearLayout>
  53. <LinearLayout
  54. android:id="@+id/activity_login_ll_layout3"
  55. android:layout_width="match_parent"
  56. android:layout_height="wrap_content"
  57. android:orientation="horizontal"
  58. android:gravity="center_vertical"
  59. android:padding="15dp"
  60. android:layout_below="@id/activity_login_ll_layout2">
  61. <TextView
  62. android:layout_width="wrap_content"
  63. android:layout_height="match_parent"
  64. android:textSize="20sp"
  65. android:gravity="center_vertical"
  66. android:textColor="#000000"
  67. android:text="密 码:"/>
  68. <com.my.myapp.customeView.CustomeEditext
  69. android:id="@+id/activity_login_et_password"
  70. android:layout_width="match_parent"
  71. android:layout_height="match_parent"
  72. android:textSize="18sp"
  73. android:inputType="textPassword"
  74. android:singleLine="true"
  75. android:background="@drawable/edittext_bg_selector"
  76. android:drawableRight="@drawable/delete_selector"
  77. android:hint="请输入密码"/>
  78. </LinearLayout>
  79. <LinearLayout
  80. android:id="@+id/activity_login_ll_layout4"
  81. android:layout_width="match_parent"
  82. android:layout_height="wrap_content"
  83. android:orientation="horizontal"
  84. android:padding="10dp"
  85. android:gravity="center_horizontal"
  86. android:layout_below="@id/activity_login_ll_layout3">
  87. <Button
  88. android:id="@+id/activity_login_btn_login"
  89. android:layout_width="0dp"
  90. android:layout_height="match_parent"
  91. android:layout_weight="1"
  92. android:text="登录"
  93. android:textSize="18sp"
  94. android:padding="15dp"
  95. android:onClick="onLocalLogin"
  96. android:background="@drawable/btn__login_style_selector"
  97. />
  98. <Button
  99. android:id="@+id/activity_login_register"
  100. android:layout_width="0dp"
  101. android:layout_height="match_parent"
  102. android:layout_weight="1"
  103. android:text="注册"
  104. android:textSize="18sp"
  105. android:layout_gravity="right"
  106. android:layout_marginLeft="30dp"
  107. android:padding="15dp"
  108. android:onClick="onLocalLogin"
  109. android:background="@drawable/btn__login_style_selector"/>
  110. </LinearLayout>
  111.  
  112. <LinearLayout
  113. android:id="@+id/activity_login_ll_layout5"
  114. android:layout_width="match_parent"
  115. android:layout_height="wrap_content"
  116. android:layout_alignParentBottom="true"
  117. android:gravity="center"
  118. android:padding="10dp"
  119. >
  120. <TextView
  121. android:id="@+id/activity_login_tv_qq"
  122. android:layout_width="wrap_content"
  123. android:layout_height="match_parent"
  124. android:text="QQ"
  125. android:gravity="center"
  126. android:drawableTop="@drawable/ssdk_oks_classic_qq"
  127. android:layout_margin="5dp"
  128. android:onClick="onOtherLogin"
  129. android:clickable="true"/>
  130. <TextView
  131. android:id="@+id/activity_login_tv_wechat"
  132. android:layout_width="wrap_content"
  133. android:layout_height="match_parent"
  134. android:text="微信"
  135. android:gravity="center"
  136. android:drawableTop="@drawable/ssdk_oks_classic_wechat"
  137. android:layout_margin="5dp"
  138. android:onClick="onOtherLogin"
  139. android:clickable="true"/>
  140. <TextView
  141. android:id="@+id/activity_login_tv_email"
  142. android:layout_width="wrap_content"
  143. android:layout_height="match_parent"
  144. android:text="微博"
  145. android:gravity="center"
  146. android:layout_margin="5dp"
  147. android:drawableTop="@drawable/ssdk_oks_classic_tencentweibo"
  148. android:onClick="onOtherLogin"
  149. android:clickable="true"/>
  150. </LinearLayout>
  151. <RelativeLayout
  152. android:layout_width="match_parent"
  153. android:layout_height="30dp"
  154. android:layout_above="@id/activity_login_ll_layout5"
  155. >
  156. <TextView
  157. android:layout_width="match_parent"
  158. android:layout_height="1dp"
  159. android:background="@android:color/darker_gray"
  160. android:layout_centerInParent="true"
  161. />
  162. <TextView
  163. android:layout_width="wrap_content"
  164. android:layout_height="wrap_content"
  165. android:text="其他账号登录"
  166. android:textColor="#CDCDC1"
  167. android:padding="7dp"
  168. android:background="#AFEEEE"
  169. android:layout_centerInParent="true"
  170. android:layout_centerHorizontal="true"
  171. android:textSize="12dp"
  172. />
  173. </RelativeLayout>
  174. </RelativeLayout>

源代码:

  1. import android.content.Context;
  2. import android.graphics.drawable.Drawable;
  3. import android.text.Editable;
  4. import android.text.TextWatcher;
  5. import android.util.AttributeSet;
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8. import android.view.View.OnFocusChangeListener;
  9. import android.view.animation.Animation;
  10. import android.view.animation.CycleInterpolator;
  11. import android.view.animation.TranslateAnimation;
  12. import android.widget.EditText;
  13.  
  14. public class ClearEditText extends EditText implements
  15. OnFocusChangeListener, TextWatcher {
  16. /**
  17. * 删除按钮的引用
  18. */
  19. private Drawable mClearDrawable;
  20. /**
  21. * 控件是否有焦点
  22. */
  23. private boolean hasFoucs;
  24.  
  25. public ClearEditText(Context context) {
  26. this(context, null);
  27. }
  28.  
  29. public ClearEditText(Context context, AttributeSet attrs) {
  30. //这里构造方法也很重要,不加这个很多属性不能再XML里面定义
  31. this(context, attrs, android.R.attr.editTextStyle);
  32. }
  33.  
  34. public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
  35. super(context, attrs, defStyle);
  36. init();
  37. }
  38.  
  39. private void init() {
  40. //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
  41. mClearDrawable = getCompoundDrawables()[2];
  42. if (mClearDrawable == null) {
  43. // throw new NullPointerException("You can add drawableRight attribute in XML");
  44. mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);
  45. }
  46.  
  47. mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
  48. //默认设置隐藏图标
  49. setClearIconVisible(false);
  50. //设置焦点改变的监听
  51. setOnFocusChangeListener(this);
  52. //设置输入框里面内容发生改变的监听
  53. addTextChangedListener(this);
  54. }
  55.  
  56. /**
  • setClearIconVisible()方法,设置隐藏和显示清除图标的方法,我们这里不是调用setVisibility()方法,setVisibility()这个方法是针对View的,我们可以调用setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)来设置上下左右的图标

  setOnFocusChangeListener(this) 为输入框设置焦点改变监听,如果输入框有焦点,我们判断输入框的值是否为空,为空就隐藏清除图标,否则就显示

  • addTextChangedListener(this) 为输入框设置内容改变监听,其实很简单呢,当输入框里面的内容发生改变的时候,我们需要处理显示和隐藏清除小图标,里面的内容长度不为0我们就显示,否是就隐藏,但这个需要输入框有焦点我们才改变显示或者隐藏,为什么要需要焦点,比如我们一个登陆界面,我们保存了用户名和密码,在登陆界面onCreate()的时候,我们把我们保存的密码显示在用户名输入框和密码输入框里面,输入框里面内容发生改变,导致用户名输入框和密码输入框里面的清除小图标都显示了,这显然不是我们想要的效果,所以加了一个是否有焦点的判断
  • setShakeAnimation(),这个方法是输入框左右抖动的方法,,当用户名错误,输入框就在哪里抖动,感觉挺好玩的,其实主要是用到一个移动动画,然后设置动画的变化率为正弦曲线

效果:

自定义EditText实现一键删除数据的更多相关文章

  1. Web jquery表格组件 JQGrid 的使用 - 7.查询数据、编辑数据、删除数据

    系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...

  2. FreeSql (九)删除数据

    删除是一个非常危险的操作,FreeSql对删除支持并不强大,仅支持了单表有条件的删除方法. 不想过多的介绍拉长删除数据的系列文章,删除数据的介绍仅此一篇. 若Where条件为空的时候执行方法,Free ...

  3. sql server编写archive通用模板脚本实现自动分批删除数据

    博主做过比较多项目的archive脚本编写,对于这种删除数据的脚本开发,肯定是一开始的话用最简单的一个delete语句,然后由于部分表数据量比较大啊,索引比较多啊,会发现删除数据很慢而且影响系统的正常 ...

  4. 使用Amazon EMR和Apache Hudi在S3上插入,更新,删除数据

    将数据存储在Amazon S3中可带来很多好处,包括规模.可靠性.成本效率等方面.最重要的是,你可以利用Amazon EMR中的Apache Spark,Hive和Presto之类的开源工具来处理和分 ...

  5. MySQL不建议delete删除数据

    InnoDB存储架构 从这张图可以看到,InnoDB存储结构主要包括两部分:逻辑存储结构和物理存储结构. 逻辑上是由表空间tablespace -> 段segment或者inode -> ...

  6. CRL快速开发框架系列教程四(删除数据)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  7. 读书笔记--SQL必知必会16--更新和删除数据

    16.1 更新数据 使用UPDATE语句更新或修改表中的数据.必须有足够的安全权限. 更新表中的特定行 更新表中的所有行 使用UPDATE时一定要细心,不要省略WHERE子句. SET命令用来将新值赋 ...

  8. 一键删除.svn文件bat脚本

    用过SVN或CVS版本控制工具的朋友,在享受着它们给我们带来的方便的同时,也许也在为这么一件事情苦恼: 如果某个目录在SVN或CVS版本控制工具的控制之下时.该目录下以及该子孙目录下都会有一个.svn ...

  9. MySQL数据库5 - 插入数据,修改数据,删除数据

    一.插入数据 1. 所有列都插入值 INSERT [INTO] TABLE_NAME VALUES(V1,V2....Vn); 特点:列值同数,列值同序 eg: insert into users v ...

随机推荐

  1. 原生JS修改标签样式为带阴影效果

    代码如下: JS部分 //点击时候,改变样式 function GetCategoryInfo(value) { var getInfo = value.toString().split(','); ...

  2. 随笔2 PAT1001.A+B Format (20)

    1001.A+B Format(20) 题目链接 1001.A+B Format (20) C++ 代码 第一次使用markdown,还不是很习惯,现在努力的在适应它 首先这道题我们很容易就可以读懂题 ...

  3. 第三次作业随笔(new)包含了补作业

    第三次作业的题目:http://www.cnblogs.com/fzuoop/p/5187275.html 第一次看到题目的时候觉得应该是挺简单的,只要把输入的那一串东西挨个判断,用数列的方法,如果碰 ...

  4. LeetCode 刷题记录

    写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...

  5. 在Ribbon中,中文换行位置不对怎么办

    在Ribbon代码的编写时,关于UI上面设置Button的中文字,经常会出现不想换行的地方换行的情况,比如下图: 我如果希望中国一行,然后只有一行怎么办呢. 网上流传的很多方法就是直接加空格,但是很遗 ...

  6. bmp格式解析

    最近一直在写图像处理的作业,好多啊 bmp格式简介 a.格式组成 1:位图头文件数据结构,它包含BMP图像文件的类型.显示内容等信息: 2:位图信息数据结构,它包含有BMP图像的宽.高.压缩方法,以及 ...

  7. java(2014版)连接数据库的工具类

    package util; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; ...

  8. OC: NSString、NSArray、NSNumber

    数组参考: 参考1  参考2  参考3 //字符串 //1.获取字符串的长度: //表情符号最少占两个字节 NSString * s = @"中文字符串

  9. zookeeper的安装和启动

    最近的手上一个项目要用到dubbo,顺便研究下zookeeper 1.下载 下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/,下载解压到D:\t ...

  10. div图片垂直居中 如何使div中图片垂直居中

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-04-03) 『此方法在ie7下,如果.box的高度为800等比较大的数值时,并不能起到垂直居中的作用.』 点评:关于图片垂 ...