Xamarin重写EditText,在右侧添加一个清除按钮图标
实现效果图:

实现代码:
1 public class ClearEditView : EditText 2 { 3 private Drawable mRightDrawable; 4 private bool hasFoucs; 5 Context context; 6 public ClearEditView(Context context) : base(context) 7 { 8 this.context = context; 9 init(); 10 } 11 public ClearEditView(Context context, IAttributeSet attrs) : base(context, attrs) 12 { 13 this.context = context; 14 init(); 15 } 16 17 public ClearEditView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) 18 { 19 this.context = context; 20 init(); 21 } 22 23 private void init() 24 { 25 //getCompoundDrawables: 26 //Returns drawables for the left, top, right, and bottom borders. 27 Drawable[] drawables = this.GetCompoundDrawables(); 28 29 //取得right位置的Drawable 30 //即我们在布局文件中设置的android:drawableRight 31 mRightDrawable = drawables[2]; 32 //设置焦点变化的监听 33 this.FocusChange += new EventHandler<FocusChangeEventArgs>(FocusChangeListenerImpl); 34 35 //设置EditText文字变化的监听 36 this.TextChanged += new EventHandler<TextChangedEventArgs>(TextWatcherImpl); 37 //初始化时让右边clean图标不可见 38 SetClearDrawableVisible(false); 39 } 40 public void TextWatcherImpl(object obj, TextChangedEventArgs e) 41 { 42 if (hasFoucs) 43 { 44 SetClearDrawableVisible(this.Text.Length > 0); 45 } 46 } 47 48 public void FocusChangeListenerImpl(object obj, FocusChangeEventArgs e) 49 { 50 51 this.hasFoucs = e.HasFocus; 52 if (hasFoucs) 53 { 54 bool isVisible = Text.ToString().Length >= 1; 55 SetClearDrawableVisible(isVisible); 56 } 57 else 58 SetClearDrawableVisible(false); 59 } 60 61 /// <summary> 62 ///当手指抬起的位置在clean的图标的区域 63 ///我们将此视为进行清除操作 64 ///Width:得到控件的宽度 65 /// e.GetX():抬起时的坐标(改坐标是相对于控件本身而言的) 66 /// TotalPaddingRight:clean的图标左边缘至控件右边缘的距离 67 /// PaddingRight:clean的图标右边缘至控件右边缘的距离 68 /// 于是: 69 /// Width - TotalPaddingRight()表示: 70 /// 控件左边到clean的图标左边缘的区域 71 /// Width - PaddingRight()表示: 72 /// 控件左边到clean的图标右边缘的区域 73 /// 所以这两者之间的区域刚好是clean的图标的区域 74 /// </summary> 75 /// <returns></returns> 76 public override bool OnTouchEvent(MotionEvent e) 77 { 78 switch (e.Action) 79 { 80 case MotionEventActions.Down: 81 82 bool isClean = (e.GetX() > (Width - TotalPaddingRight)) && 83 (e.GetX() < (Width - PaddingRight)); 84 if (isClean) 85 { 86 SetText(string.Empty, BufferType.Normal); 87 } 88 break; 89 90 default: 91 break; 92 } 93 return base.OnTouchEvent(e); 94 } 95 96 //隐藏或者显示右边clean的图标 97 public void SetClearDrawableVisible(bool isVisible) 98 { 99 Drawable rightDrawable; 100 if (isVisible) 101 { 102 rightDrawable = mRightDrawable; 103 } 104 else 105 { 106 rightDrawable = null; 107 } 108 //使用代码设置该控件left, top, right, and bottom处的图标 109 SetCompoundDrawables(GetCompoundDrawables()[0], GetCompoundDrawables()[1], 110 rightDrawable, GetCompoundDrawables()[3]); 111 } 112 }
UI引用布局:
1 <XQY.ClearEditView 2 android:id="@+id/et_user_name" 3 android:paddingLeft="28dip" 4 android:layout_width="fill_parent" 5 android:layout_height="55dip" 6 android:hint="邮箱" 7 android:textColorHint="#a9a9a9" 8 android:gravity="center_vertical" 9 android:textSize="17sp" 10 android:background="@drawable/et_radius" 11 android:drawableRight="@mipmap/delete" 12 android:paddingRight="16dip"/>
因为一无所有,所以无所畏惧。
不是什么东西,人出生就懂的。
——蚁族的奋斗
浙公网安备 33010602011771号