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 touch事件一种解释
  2. Android图案密码,手势锁源码解析
  3. Android Touch事件传递机制引发的血案
  4. Android的多媒体框架Opencore代码阅读
  5. Android应用程序启动过程源代码分析
  6. Android(Java):Android 事件分发机制
  7. android 回车键事件编程
  8. Android 事件分发机制源码

随机推荐

  1. Android(安卓)Drawable 系列——ClipDraw
  2. android tools:
  3. LinearLayout(线性布局)
  4. android源码导入
  5. Android(安卓)UI测量、布局、绘制过程探
  6. android中的xml处理
  7. 2010.12.28(3)——— android alertDialo
  8. android bundle 对象 序列化
  9. Xamarin开发Android时Visual Studio 2012
  10. Android(安卓)Studio默认创建的view学习