package com.yunlong.appstore.common;
/**
* Created by qianqian on 2017/1/6.
* 实现drawableleft, ..right的点击事件
*/
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
public class EditTextDrawableClick extends EditText {
private DrawableLeftListener mLeftListener;
private DrawableRightListener mRightListener;
private DrawableTopListener mTopListener;
private DrawableBottomListener mBottomListener;
private final int DRAWABLE_LEFT = 0;
private final int DRAWABLE_TOP = 1;
private final int DRAWABLE_RIGHT = 2;
private final int DRAWABLE_BOTTOM = 3;
public EditTextDrawableClick(Context context) {
super(context);
}
public EditTextDrawableClick(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextDrawableClick(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* event.getRawX(),相对于左边界的绝对坐标,以左上角为(0,0)
* event.getX(),相对于自身的坐标,以该空间的左上角为(0,0)
* getLeft(),相当于margin,控件左边界相对于父控件的距离
* getPaddingLeft,相当于padding,控件中元素相对于控件的间距
* getBounds().width(),获取元素绘制区域的宽度
* drawableRight.getIntrinsicWidth(),获取drawable的实际宽度
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
//监听手指抬起时候的动作
if (event.getAction() == MotionEvent.ACTION_UP) {
//首先判断是否被实例化了
if (mRightListener != null) {
//getCompoundDrawables() 可以获取一个长度为4的drawable数组,存放drawableLeft,Right,Top,Bottom四个图片资源对象
Drawable drawableRight = getCompoundDrawables()[DRAWABLE_RIGHT];
//首先确保得到的drawable对象不为空,然后得到本次点击事件的x轴坐标,如果>当前控件宽度-控件右间距-drawable实际展示大小,
// 那么说明就是点击在这个drawable上面了。其实就是算该drawable最左边的x坐标,drawableRight.getIntrinsicWidth()==drawableRight.getBounds().width()等于图标的宽度
if (drawableRight != null && event.getX() > getWidth() - getPaddingRight() - drawableRight.getIntrinsicWidth()) {
// if (drawableRight != null && event.getRawX() >= (getRight() - drawableRight.getBounds().width())) {
Log.d("DEBUG", "getX=" + event.getX() + "getY=" + event.getY() + "\n" + "getRawX=" + event.getRawX()
+ "getRawY=" + event.getY() + "\n" + "test=" + getLeft() + "\n" + "test:hehe" + getPaddingLeft());
mRightListener.onDrawableRightClick(this);
}
}
if (mLeftListener != null) {
Drawable drawableLeft = getCompoundDrawables()[DRAWABLE_LEFT];
//getX是相对于控件本身的左上角的x坐标,<=控件左边距+图片对象实际的宽度.这边的getLeft相当于margin,getPaddingLeft相当于padding
if (drawableLeft != null && event.getX() <= getLeft() + drawableLeft.getIntrinsicWidth()) {
// if (drawableLeft != null && event.getRawX() <= (getLeft() + drawableLeft.getBounds().width())) {
mLeftListener.onDrawableLeftClick(this);
}
}
//自己去理解,比较容易的
if (mTopListener != null) {
Drawable drawableTop = getCompoundDrawables()[DRAWABLE_TOP];
if (drawableTop != null && event.getY() <= getTop() + drawableTop.getIntrinsicHeight()) {
mTopListener.onDrawableTopClick(this);
}
}
//自己去理解,比较容易的
if (mBottomListener != null) {
Drawable drawableBottom = getCompoundDrawables()[DRAWABLE_BOTTOM];
if (drawableBottom != null && event.getX() > getHeight() - drawableBottom.getIntrinsicWidth()) {
mBottomListener.onDrawableBottomClick(this);
}
}
}
return super.onTouchEvent(event);
}
//相应的控件注册的事件
public void setDrawableLeftListener(DrawableLeftListener mListener) {
this.mLeftListener = mListener;
}
//相应的控件注册的事件
public void setDrawableRightListener(DrawableRightListener mListener) {
this.mRightListener = mListener;
}
//相应的控件注册的事件
public void setDrawableTopListener(DrawableTopListener mListener) {
this.mTopListener = mListener;
} //相应的控件注册的事件
public void setDrawableBottomListener(DrawableBottomListener mListener) {
this.mBottomListener = mListener;
}
/**
* 回调接口需要,需要复写
*/
public interface DrawableRightListener {
void onDrawableRightClick(View view);
}
/**
* 回调接口
*/
public interface DrawableLeftListener {
void onDrawableLeftClick(View view);
}
/**
* 回调接口
*/
public interface DrawableTopListener {
void onDrawableTopClick(View view);
}
/**
* 回调接口
*/
public interface DrawableBottomListener {
void onDrawableBottomClick(View view);
}
}
EditText处理Drawble点击事件
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前两天做应用的时候TextView点击事件一直不正常,需要点击两次才能触发,今天发现是因为我设置了android:...
- 这里自定义了一个RightPicClickEditText,继承AppCompatEditText,并且在其三个构...
- 1、求学 小时候,我特别羡慕小哥哥、小姐姐们能去上学。所以,在我整日的纠缠之下,我妈实在扭不过我,于是就把我送去上...