经过大神指导,上面封装的还不够全面,触摸事件应该也放进自定义中去,那么问题来了,怎么区分呢!,这就涉及到了自定义属性的介绍了
我通过设置属性来判断在onTouch事件中应该进行什么操作,接下来看看改良的代码
首先在attrs中设置样式
1 <declare-styleable name="drawrighteditview"> 2 <attr name="edit_type" format="string" /> 3 </declare-styleable>
然后在布局中设置样式,先导入xmlns:attrs="http://schemas.android.com/apk/res-auto"
1 <com.asuka.android.asukaandroid.demo.views.DrawRightEditText 2 android:id="@+id/name" 3 android:layout_width="match_parent" 4 android:layout_height="30dp" 5 android:layout_centerVertical="true" 6 android:layout_toRightOf="@+id/iv1" 7 android:background="@null" 8 android:hint="请输入账号" 9 android:textColor="#000"
//设置属性 10 attrs:edit_type="name" 11 android:drawableRight="@drawable/icon_delete" 12 android:layout_marginLeft="10dp" 13 android:layout_marginRight="10dp" 14 android:textColorHint="#8d8d94" 15 android:textSize="16sp" />
再来看看自定义代码获取属性,实现在OnToch事件的操作
1 public class DrawRightEditText extends EditText { 2 private Drawable mClearDrawable; 3 private String edit_type; 4 private boolean isSee = false;//密码是否可见 5 private static final int PASSWORD_MINGWEN = 0x90; 6 private static final int PASSWORD_MIWEN = 0x81; 7 String myNamespace = "http://schemas.android.com/apk/res-auto"; 8 public DrawRightEditText(Context context) { 9 this(context, null); 10 } 11 12 public DrawRightEditText(Context context, AttributeSet attrs) { 13 //这里构造方法也很重要,不加这个很多属性不能再XML里面定义 14 this(context, attrs, android.R.attr.editTextStyle); 15 } 16 17 public DrawRightEditText(Context context, AttributeSet attrs, int defStyle) { 18 super(context, attrs, defStyle); 19 //获取属性 20 edit_type = attrs.getAttributeValue(myNamespace, 21 "edit_type"); 22 init(); 23 } 24 private void init(){ 25 //获取EditText的DrawableRight,getCompoundDrawables()获取Drawable的四个位置的数组 26 mClearDrawable = getCompoundDrawables()[2]; 27 //设置图标的位置以及大小,getIntrinsicWidth()获取显示出来的大小而不是原图片的带小 28 mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth()+10, mClearDrawable.getIntrinsicHeight()+10); 29 //默认设置隐藏图标 30 setClearIconVisible(false); 31 //设置输入框里面内容发生改变的监听 32 addTextChangedListener(new TextWatcher() { 33 @Override 34 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 35 36 } 37 38 @Override 39 public void onTextChanged(CharSequence s, int start, int before, int count) { 40 setClearIconVisible(s.length() > 0); 41 } 42 43 @Override 44 public void afterTextChanged(Editable s) { 45 46 } 47 }); 48 } 49 50 /** 51 * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去 52 * @param visible 53 */ 54 protected void setClearIconVisible(boolean visible) { 55 Drawable right = visible ? mClearDrawable : null; 56 setCompoundDrawables(getCompoundDrawables()[0], 57 getCompoundDrawables()[1], right, getCompoundDrawables()[3]); 58 } 59 /** 60 * 设置晃动动画 61 */ 62 public void setShakeAnimation(){ 63 this.startAnimation(shakeAnimation(3)); 64 } 65 /** 66 * 晃动动画 67 * @param counts 1秒钟晃动多少下 68 * @return 69 */ 70 public static Animation shakeAnimation(int counts){ 71 Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); 72 translateAnimation.setInterpolator(new CycleInterpolator(counts)); 73 translateAnimation.setDuration(1000); 74 return translateAnimation; 75 } 76 77 @Override 78 public boolean onTouchEvent(MotionEvent event) { 79 if (event.getAction() == MotionEvent.ACTION_UP) { 80 if (getCompoundDrawables()[2] != null) { 81 //getTotalPaddingRight()图标左边缘至控件右边缘的距离 82 //getWidth() - getTotalPaddingRight()表示从最左边到图标左边缘的位置 83 //getWidth() - getPaddingRight()表示最左边到图标右边缘的位置 84 boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) 85 && (event.getX() < ((getWidth() - getPaddingRight()))); 86 87 if (touchable) {
//根据属性判断操作 88 if (edit_type.equals("name")){ 89 this.setText(""); 90 }else if (edit_type.equals("password")){ 91 if (isSee) { 92 //设置不可见 93 this.setInputType(PASSWORD_MIWEN);//密文 94 this.setSelection(this.length());//设置光标显示 95 } else { 96 //设置可见 97 this.setInputType(PASSWORD_MINGWEN);//明文 98 this.setSelection(this.length());//设置光标显示 99 } 100 isSee = !isSee; 101 } 102 } 103 } 104 } 105 return super.onTouchEvent(event); 106 } 107 }