EditTextView

  1 package com.egojit.android.sops.views.EditText;
  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.animation.Animation;
 10 import android.view.animation.CycleInterpolator;
 11 import android.view.animation.TranslateAnimation;
 12 import android.widget.EditText;
 13 
 14 import com.asuka.android.asukaandroid.R;
 15 
 16 /**
 17  * 备注:
 18  * 作者:王莹
 19  * 时间:2017/4/25.
 20  */
 21 
 22 public class EditTextView extends EditText {
 23     private Drawable mClearDrawable;
 24     private String edit_type;
 25     private boolean isSee = false;//密码是否可见
 26     private static final int PASSWORD_MINGWEN = 0x90;
 27     private static final int PASSWORD_MIWEN = 0x81;
 28     private int mDrawablePadding = 16;
 29     String myNamespace = "http://schemas.android.com/apk/res-auto";
 30 
 31     public EditTextView(Context context) {
 32         this(context, null);
 33     }
 34 
 35     public EditTextView(Context context, AttributeSet attrs) {
 36         this(context, attrs, android.R.attr.editTextStyle);
 37     }
 38 
 39     public EditTextView(Context context, AttributeSet attrs, int defStyle) {
 40         super(context, attrs, defStyle);
 41 
 42         edit_type = attrs.getAttributeValue(myNamespace,
 43                 "edit_type");
 44         init();
 45     }
 46 
 47     private void init() {
 48         //获取EditText的DrawableRight,getCompoundDrawables()获取Drawable的四个位置的数组
 49         mClearDrawable = getCompoundDrawables()[2];
 50         if (mClearDrawable == null) {
 51             if (edit_type.equals("name")) {
 52 //          默认显示的是删除按钮
 53                 mClearDrawable = getResources().getDrawable(R.drawable.delete);
 54             } else if (edit_type.equals("password")) {
 55                 // 默认显示的是明文密文按钮
 56                 mClearDrawable = getResources().getDrawable(R.drawable.icon_eye);
 57             }
 58         }
 59         //设置图标的位置以及大小,getIntrinsicWidth()获取显示出来的大小而不是原图片的大小
 60         mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth() + 10, mClearDrawable.getIntrinsicHeight() + 10);
 61         //默认设置隐藏图标
 62         setClearIconVisible(false);
 63         //设置输入框里面内容发生改变的监听
 64         addTextChangedListener(new TextWatcher() {
 65             @Override
 66             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 67 
 68             }
 69 
 70             @Override
 71             public void onTextChanged(CharSequence s, int start, int before, int count) {
 72                 setClearIconVisible(s.length() > 0);
 73             }
 74 
 75             @Override
 76             public void afterTextChanged(Editable s) {
 77 
 78             }
 79         });
 80     }
 81 
 82     /**
 83      * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
 84      *
 85      * @param visible
 86      */
 87     protected void setClearIconVisible(boolean visible) {
 88         Drawable right = visible ? mClearDrawable : null;
 89         setCompoundDrawables(getCompoundDrawables()[0],
 90                 getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
 91     }
 92 
 93     /**
 94      * 设置晃动动画
 95      */
 96     public void setShakeAnimation() {
 97         this.startAnimation(shakeAnimation(3));
 98     }
 99 
100     /**
101      * 晃动动画
102      *
103      * @param counts 1秒钟晃动多少下
104      * @return
105      */
106     public static Animation shakeAnimation(int counts) {
107         Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
108         translateAnimation.setInterpolator(new CycleInterpolator(counts));
109         translateAnimation.setDuration(1000);
110         return translateAnimation;
111     }
112 
113     @Override
114     public boolean onTouchEvent(MotionEvent event) {
115         if (event.getAction() == MotionEvent.ACTION_UP) {
116             if (getCompoundDrawables()[2] != null) {
117                 //getTotalPaddingRight()图标左边缘至控件右边缘的距离
118                 //getWidth() - getTotalPaddingRight()表示从最左边到图标左边缘的位置
119                 //getWidth() - getPaddingRight()表示最左边到图标右边缘的位置
120                 boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
121                         && (event.getX() < ((getWidth() - getPaddingRight())));
122 
123                 if (touchable) {
124                     if (edit_type != null) {
125                         if (edit_type.equals("name")) {
126                             this.setText("");
127                         } else if (edit_type.equals("password")) {
128                             if (isSee) {
129                                 //设置不可见
130                                 this.setInputType(PASSWORD_MIWEN);//密文
131                                 this.setSelection(this.length());//设置光标显示
132                                 mClearDrawable = getResources().getDrawable(R.drawable.loginmiwen);
133                                 setIcon(mClearDrawable);
134                             } else {
135                                 //设置可见
136                                 this.setInputType(PASSWORD_MINGWEN);//明文
137                                 this.setSelection(this.length());//设置光标显示
138                                 mClearDrawable = getResources().getDrawable(R.drawable.loginmingwen);
139                                 setIcon(mClearDrawable);
140                             }
141                             isSee = !isSee;
142                         }
143                     } else {
144                         //默认为进行删除操作
145                         this.setText("");
146                     }
147                 }
148             }
149         }
150         return super.onTouchEvent(event);
151     }
152 
153     private void setIcon(Drawable mDeleIcon) {
154         //设置图标的位置以及大小,getIntrinsicWidth()获取显示出来的大小而不是原图片的大小
155         mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth() + 10, mClearDrawable.getIntrinsicHeight() + 10);
156         setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[0], mDeleIcon, getCompoundDrawables()[0]);
157     }
158 }

 

posted on 2017-12-26 14:01  oooo呼呼  阅读(733)  评论(0编辑  收藏  举报