<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 ;        }    }


更多相关文章

  1. Android(安卓)实现事件监听的两个方法
  2. Fragment中添加tabs
  3. Android(安卓)- SwitchButton开关按钮
  4. Android利用BroadcastReceiver实现短信监听
  5. 12.8 SeekBar和RatingBar
  6. Android(安卓)滑动渐变 扩展效果 免费专区
  7. android实现摇一摇事件监听
  8. Android如何用Rxjava监听BroadcastReceiver
  9. RadioGroup控件(接口类监听事件)

随机推荐

  1. 学习笔记-Android(安卓)Gallery实现
  2. Android的OpenGL学习笔记(6)
  3. Android(安卓)SDK 国内镜像
  4. android wifi 相关方面整理
  5. Android实现控件View移动控制。
  6. android studio设置debug.keystore
  7. 对TextView ImageView 设置了drawable se
  8. Android(安卓)命令行手动编译打包过程图
  9. Android监控外接USB设备和获取USB等设备
  10. 5分钟理解Android中的上下文!!!