Android 设置DrawableRight和DrawableLeft 点击事件

Android的TextView有个DrawableLeft和DrawableRight属性,UI布局中经常会用到。比如登陆界面,用户名和密码前面的图像,就是用DrawableLeft来设置的。

但比较郁闷的是,Android并没有为DrawableLeft和DrawableRight提供监听点击事件的api,但这个需求是很常见的,比如输入密码的时候,点击右边的眼睛,密码变为明文。

其实思路很简单,我们只要重写EditText的onTouchEvent,捕捉到点击事件,然后判断用户点击的位置是否点击到图标即可,不废话,用代码说话。

详细代码请见Github(若有更新只在github更新)

先创建一个类,继承EditText,如:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 /** * 加强版的EditText,可以响应DrawableLeft 和 DrawableRight的点击事件 * 要实现响应点击,先设置setDrawableListener * @author xing * @version 1.1 */ public class XEditText extends EditText { private DrawableLeftListener mLeftListener ; private DrawableRightListener mRightListener ; final int DRAWABLE_LEFT = 0 ; final int DRAWABLE_TOP = 1 ; final int DRAWABLE_RIGHT = 2 ; final int DRAWABLE_BOTTOM = 3 ; @SuppressLint ( "NewApi" ) public XEditText ( Context context , AttributeSet attrs , int defStyleAttr , int defStyleRes ) { super ( context , attrs , defStyleAttr , defStyleRes ) ; } public XEditText ( Context context , AttributeSet attrs , int defStyleAttr ) { super ( context , attrs , defStyleAttr ) ; } public XEditText ( Context context , AttributeSet attrs ) { super ( context , attrs ) ; } public XEditText ( Context context ) { super ( context ) ; } public void setDrawableLeftListener ( DrawableLeftListener listener ) { this . mLeftListener = listener ; } public void setDrawableRightListener ( DrawableRightListener listener ) { this . mRightListener = listener ; } public interface DrawableLeftListener { public void onDrawableLeftClick ( View view ) ; } public interface DrawableRightListener { public void onDrawableRightClick ( View view ) ; } @SuppressLint ( "ClickableViewAccessibility" ) @Override public boolean onTouchEvent ( MotionEvent event ) { switch ( event . getAction ( ) ) { case MotionEvent . ACTION_UP : if ( mRightListener != null ) { Drawable drawableRight = getCompoundDrawables ( ) [ DRAWABLE_RIGHT ] ; if ( drawableRight != null && event . getRawX ( ) >= ( getRight ( ) - drawableRight . getBounds ( ) . width ( ) ) ) { mRightListener . onDrawableRightClick ( this ) ; return true ; } } if ( mLeftListener != null ) { Drawable drawableLeft = getCompoundDrawables ( ) [ DRAWABLE_LEFT ] ; if ( drawableLeft != null && event . getRawX ( ) <= ( getLeft ( ) + drawableLeft . getBounds ( ) . width ( ) ) ) { mLeftListener . onDrawableLeftClick ( this ) ; return true ; } } break ; } return super . onTouchEvent ( event ) ; } }

上面代码初始化了一些数据,设置了一个DrawableListener接口,用于回调。目前只设置了DrawableLeft和DrawableRight,至于Top和Bottom大家自己设置吧,俺暂时没用到,就偷懒不写了。

使用的方法就不用多说了吧,写一个实现类用户回调就行了,大家灵活运用即可。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 private class DrawableRightClickListener implements DrawableListener { @ Override public void onDrawaRightbleClick ( View view ) { if ( ! mIsShow ) { mPassword . setCompoundDrawablesWithIntrinsicBounds ( R . drawable . ic_lock , 0 , R . drawable . ic_eye_red , 0 ) ; mPassword . setInputType ( InputType . TYPE_TEXT_VARIATION_VISIBLE_PASSWORD ) ; } else { mPassword . setCompoundDrawablesWithIntrinsicBounds ( R . drawable . ic_lock , 0 , R . drawable . ic_eye , 0 ) ; mPassword . setInputType ( InputType . TYPE_CLASS_TEXT | InputType . TYPE_TEXT_VARIATION_PASSWORD ) ; } mIsShow = ! mIsShow ; } @ Override public void onDrawableLeftClick ( View view ) { System . out . println ( "left clicked" ) ; } }

更多相关文章

  1. Android中设置控件可见与不可见
  2. Android(安卓)View系列 - 坐标系
  3. Android(安卓)Studio打不开的问题
  4. 饭后Android(安卓)第一餐-NavigationView+Toolbar(NavigationView
  5. Android启动模式之singleTask解析
  6. android 属性汇总
  7. Android(安卓)touch事件一种解释
  8. TextView使用完全讲解
  9. Android(安卓)DEV : setOnClickListener() vs. android:onClick

随机推荐

  1. Android(安卓)启动暗码打开指令窗口
  2. android 软件键盘
  3. Android(安卓)播放音乐文件与视频文件
  4. 异常ExceptionInInitializerError解决方
  5. Android(安卓)自定义 View 实例 随手拖动
  6. 图库选择
  7. Android(安卓)查看大图(直接复制可以用)
  8. Android(安卓)Studio音乐播放器and视频播
  9. android 下拉刷新控件
  10. ViewPager初步用法(二)