有趣的EditView为空时的抖动效果(用户名和密码)--第三方开源--ClearEditText

ClearEditText在github上的链接地址是:https://github.com/zhangphil/ClearEditText

用法十分简单,在布局中使用ClearEditText,在JAVA中setShakeAnimation()即可。

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="#95CAE4">
 6
 7
 8     <com.example.clearedittext.ClearEditText
 9         android:id="@+id/username"
10         android:layout_marginTop="60dp"
11         android:layout_width="fill_parent"
12         android:background="@drawable/login_edittext_bg"
13         android:drawableLeft="@drawable/icon_user"
14         android:layout_marginLeft="10dip"
15         android:layout_marginRight="10dip"
16         android:singleLine="true"
17         android:drawableRight="@drawable/delete_selector"
18         android:hint="输入用户名"
19         android:layout_height="wrap_content" >
20
21     </com.example.clearedittext.ClearEditText>
22
23     <com.example.clearedittext.ClearEditText
24         android:id="@+id/password"
25         android:layout_marginLeft="10dip"
26         android:layout_marginRight="10dip"
27         android:layout_marginTop="10dip"
28         android:drawableLeft="@drawable/account_icon"
29         android:hint="输入密码"
30         android:singleLine="true"
31         android:password="true"
32         android:drawableRight="@drawable/delete_selector"
33         android:layout_width="fill_parent"
34         android:layout_height="wrap_content"
35         android:layout_below="@id/username"
36         android:background="@drawable/login_edittext_bg" >
37     </com.example.clearedittext.ClearEditText>
38
39     <Button
40         android:id="@+id/login"
41         android:layout_width="fill_parent"
42         android:layout_height="wrap_content"
43         android:layout_marginLeft="10dip"
44         android:layout_marginRight="10dip"
45         android:background="@drawable/login_button_bg"
46         android:textSize="18sp"
47         android:textColor="@android:color/white"
48         android:layout_below="@+id/password"
49         android:layout_marginTop="25dp"
50         android:text="登录" />
51
52 </RelativeLayout>
  1 package com.example.clearedittext;
  2
  3 import android.content.Context;
  4 import android.graphics.drawable.Drawable;
  5 import android.text.Editable;
  6 import android.text.TextWatcher;
  7 import android.util.AttributeSet;
  8 import android.view.MotionEvent;
  9 import android.view.View;
 10 import android.view.View.OnFocusChangeListener;
 11 import android.view.animation.Animation;
 12 import android.view.animation.CycleInterpolator;
 13 import android.view.animation.TranslateAnimation;
 14 import android.widget.EditText;
 15
 16 public class ClearEditText extends EditText implements
 17         OnFocusChangeListener, TextWatcher {
 18     /**
 19      * 删除按钮的引用
 20      */
 21     private Drawable mClearDrawable;
 22     /**
 23      * 控件是否有焦点
 24      */
 25     private boolean hasFoucs;
 26
 27     public ClearEditText(Context context) {
 28         this(context, null);
 29     }
 30
 31     public ClearEditText(Context context, AttributeSet attrs) {
 32         //这里构造方法也很重要,不加这个很多属性不能再XML里面定义
 33         this(context, attrs, android.R.attr.editTextStyle);
 34     }
 35
 36     public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
 37         super(context, attrs, defStyle);
 38         init();
 39     }
 40
 41
 42     private void init() {
 43         //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片,右边位置图片
 44         mClearDrawable = getCompoundDrawables()[2];
 45         if (mClearDrawable == null) {
 46 //            throw new NullPointerException("You can add drawableRight attribute in XML");
 47             mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);
 48         }
 49
 50         mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
 51         //默认设置隐藏图标
 52         setClearIconVisible(false);
 53         //设置焦点改变的监听
 54         setOnFocusChangeListener(this);
 55         //设置输入框里面内容发生改变的监听
 56         addTextChangedListener(this);
 57     }
 58
 59
 60     /**
 61      * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件
 62      * 当我们按下的位置 在  EditText的宽度 - 图标到控件右边的间距 - 图标的宽度  和
 63      * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑
 64      */
 65     @Override
 66     public boolean onTouchEvent(MotionEvent event) {
 67         if (event.getAction() == MotionEvent.ACTION_UP) {
 68             if (getCompoundDrawables()[2] != null) {
 69
 70                 boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
 71                         && (event.getX() < ((getWidth() - getPaddingRight())));
 72
 73                 if (touchable) {
 74                     this.setText("");
 75                 }
 76             }
 77         }
 78
 79         return super.onTouchEvent(event);
 80     }
 81
 82     /**
 83      * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
 84      */
 85     @Override
 86     public void onFocusChange(View v, boolean hasFocus) {
 87         this.hasFoucs = hasFocus;
 88         if (hasFocus) {
 89             setClearIconVisible(getText().length() > 0);
 90         } else {
 91             setClearIconVisible(false);
 92         }
 93     }
 94
 95
 96     /**
 97      * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
 98      * @param visible
 99      */
100     protected void setClearIconVisible(boolean visible) {
101         Drawable right = visible ? mClearDrawable : null;
102         setCompoundDrawables(getCompoundDrawables()[0],
103                 getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
104     }
105
106
107     /**
108      * 当输入框里面内容发生变化的时候回调的方法
109      */
110     @Override
111     public void onTextChanged(CharSequence s, int start, int count,
112             int after) {
113                 if(hasFoucs){
114                     setClearIconVisible(s.length() > 0);
115                 }
116     }
117
118     @Override
119     public void beforeTextChanged(CharSequence s, int start, int count,
120             int after) {
121
122     }
123
124     @Override
125     public void afterTextChanged(Editable s) {
126
127     }
128
129
130     /**
131      * 设置晃动动画
132      */
133     public void setShakeAnimation(){
134         this.setAnimation(shakeAnimation(5));
135     }
136
137
138     /**
139      * 晃动动画
140      * @param counts 1秒钟晃动多少下
141      * @return
142      */
143     public static Animation shakeAnimation(int counts){
144         Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
145         translateAnimation.setInterpolator(new CycleInterpolator(counts));
146         translateAnimation.setDuration(1000);
147         return translateAnimation;
148     }
149
150
151 }
时间: 2024-07-29 23:55:35

有趣的EditView为空时的抖动效果(用户名和密码)--第三方开源--ClearEditText的相关文章

关于如何快速清除,登陆文件共享,域等时缓存的认证用户名和密码的方法

每次我们用用户名和密码登陆文件共享服务器或登陆域时,经常会碰到没有保存用户名和密码,但是又想快速使用其他用户登录,因为有缓存,导致我们再登陆的时候不会弹出认证对话框,而是直接进去了,这样我们就不能更换认证信息.此时一般的做法,可以重启计算机解决.若是登陆时选择保存了,则要先进控制中心->用户管理->管理凭据->清除掉保存的项. 快速的办法是我的电脑右键->管理->服务里,把workstation service restart,这样就可以不用重启电脑,快速切换了.

解决Git 每次提交时都要输入用户名和密码的缓存机制的设置

/*********************************************************************  * Author  : Samson  * Date    : 05/23/2015  * Test platform:  *              gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2  *              GNU bash, 4.3.11(1)-release (x86_64-pc-linux-gnu)

//可以不保存在session中, 并且前面我保存在request,这里session也可以获取 chain.doFilter(request, response); //只有登录名不为空时放行,防止直接登录 成功的页面

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httprequest = (HttpServletRequest)request; HttpServletResponse httpresponse = (HttpServletResponse)res

VB6.0中,日期、时间控件不允许为空时,采用文本框与日期、时间控件相互替换赋值(解决方案)

VB6.0中,日期.时间控件不允许为空时,采用文本框与日期.时间控件相互替换赋值,或许是一个不错的选择. 实现效果如下图: 代码如下: 文本框txtStopTime1 时间框DTStopTime1 格式3 - dtpCustom  HH:mm:ss Private Sub Form_Load()       txtStopTime1.ZOrder       DTStopTime1.Top = txtStopTime1.Top       DTStopTime1.Left = txtStopTi

nvl,空时的推断和取值

nvl NVL的概念 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:假设string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值,假设两个參数的都为NULL ,则返回NULL. 注意事项:string1和replace_with必须为同一数据类型,除非显示的使用TO_CHAR函数. 例:NVL(TO_CHAR(numeric_column), 'some string') 当中numeri

img, script, link 的 src/href 为空时的bug

重复加载 这个 bug 并不新鲜.早在 2009 年,Nicholas C. Zakas 就发现了空 src 的危害性:Empty image src can destroy your site. Nicholas 的发现可以概括为一句话:img, script, link 的 src/href 为空时,有可能会导致冗余请求. 今天这个 bug 的起因,可以补充 Nicholas 的发现:CSS 里,background url 为空时,也有可能会导致冗余请求. 除了空值,还有一个值也会出问题:

WPF 设置TextBox为空时,背景为文字提示。

<TextBox FontSize="17" Height="26" Margin="230,150,189,0" Name="txt_Account" VerticalAlignment="Top" Foreground="Indigo" TabIndex="0" BorderThickness="1"> <TextBox.Re

当对象或对象属性为空时,如何安全给对象或对象属性添加默认值

今天遇到的问题,也是写代码的习惯问题,逻辑没有问题,但不规范,也不安全, 容易出现漏洞. 先将代码贴出: String isPrintLogo = vodInfoDto.getIsPrintLogo();            if(!isPrintLogo.equalsIgnoreCase("0")){               isPrintLogo="1";                demandVideoInfo.setIsPrintLogo(isPr

dedecms当二级栏目为空时,不显示同级栏目的修改方法

我们在使用织梦系统制作网站时经常会遇到网站栏目较多,显示当前栏目下的二级与三级栏目时,使用栏目嵌套标签,但是当三级栏目为空时,会显示同级栏目.从用户体验角度出发,常理情况下也是需要空白的,即二级栏目下的三级栏目如为空时,则不显示.那么如何让织梦{dede:channel type='son'}无子栏目时不显示同级栏目呢?今天青岛做网站就跟大家分享一下解决方法? 方法一: 打开:include\taglib\channel.lib.php文件. 找到 if($type=='son' && $