Android 设置DrawableRight和DrawableLeft 点击事件
2015-12-09 15:06
465 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="java">package com.ulucu.xview; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; /** * Created by lbin on 2015/12/9. */ 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; public XEditText(Context context, AttributeSet attrs) { super(context, attrs); } public XEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } 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) ; } @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); } }
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">然后可以写一个自定义监听类。</span>
private class DrawableRightClickListener implements XEditText.DrawableRightListener { @Override public void onDrawableRightClick(View view) { if (!mIsShow) { network_password.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_password, 0, R.mipmap.btn_nosee, 0) ; network_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) ; } else { network_password.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_password, 0, R.mipmap.btn_see, 0) ; network_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) ; } mIsShow = !mIsShow ; } }
相关文章推荐
- 一种将Android应用打成架包提供给第三方使用的方法
- 自定义Android流布局实现推荐标签功能
- Android Studio 中提交时建议过滤的文件
- Android选择文件及通过Uri获取文件路径
- 使用Android Studio创建Android项目
- Android LogCat日志过滤
- Android Ril层浅析
- Android异步消息机制及源码分析
- Android调用手机相机与手机图库选择用户头像
- Android Studio 使用中遇到的问题和解决方案
- android下获得手机自带存储的路径
- Android重要控件概览(下)
- phonegap混合开发之环境安装
- 风格化Action Bar
- Android EventBus实战
- Android初学习 - ViewPager与Fragment初步
- Android开发周报:Android Studio2.0发布、Activity动态创建
- Android初学习 - 获取手机状态和监听手机来电状态
- Android应用程序资源管理器
- Android中获取apk安装包信息的方法