直接上自定义的 TextView
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 androidx.appcompat.widget.AppCompatTextView;
public class TextViewDrawableClick extends AppCompatTextView {
private static final String TAG = "TextViewDrawableClick";
private TextViewDrawableClick.OnDrawableLeftClickListener mLeftListener;
private TextViewDrawableClick.OnDrawableRightClickListener mRightListener;
private TextViewDrawableClick.OnDrawableTopClickListener mTopListener;
private TextViewDrawableClick.OnDrawableBottomClickListener mBottomListener;
private TextViewDrawableClick.OnTextClickListener mTextListener;//中间的文字点击事件
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 TextViewDrawableClick(Context context) {
super(context);
}
public TextViewDrawableClick(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewDrawableClick(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())) {
mRightListener.onDrawableRightClick(this);
return true;
}
}
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);
return true;
}
}
//自己去理解,比较容易的
if (mTopListener != null) {
Drawable drawableTop = getCompoundDrawables()[DRAWABLE_TOP];
if (drawableTop != null && event.getY() <= getTop() + drawableTop.getIntrinsicHeight()) {
mTopListener.onDrawableTopClick(this);
return true;
}
}
//自己去理解,比较容易的
if (mBottomListener != null) {
Drawable drawableBottom = getCompoundDrawables()[DRAWABLE_BOTTOM];
if (drawableBottom != null && event.getX() > getHeight() - drawableBottom.getIntrinsicWidth()) {
mBottomListener.onDrawableBottomClick(this);
return true;
}
}
if(mTextListener != null){
Log.d("xxx","mTextListener != null");
Drawable drawableLeft = getCompoundDrawables()[DRAWABLE_LEFT];
// Drawable drawableTop = getCompoundDrawables()[DRAWABLE_TOP];
Drawable drawableRight = getCompoundDrawables()[DRAWABLE_RIGHT];
// Drawable drawableBottom = getCompoundDrawables()[DRAWABLE_BOTTOM];
//左右同时有图标
if((drawableLeft != null &&
event.getX() >= getLeft() + drawableLeft.getIntrinsicWidth())
&&
(drawableRight != null &&
event.getX() <= getWidth() - getPaddingRight() - drawableRight.getIntrinsicWidth())){
mTextListener.onTextClick(this);
Log.d("xxx","onTextClick");
}
//只有左边有图标
if(drawableLeft != null &&
event.getX() >= getLeft() + drawableLeft.getIntrinsicWidth()){
mTextListener.onTextClick(this);
}
//只有右边有图标
if(drawableRight != null &&
event.getX() <= getWidth() - getPaddingRight() - drawableRight.getIntrinsicWidth()){
mTextListener.onTextClick(this);
}
//上下同时有图标...
return true;
}
}
return super.onTouchEvent(event);
}
//相应的控件注册的事件
public void setOnDrawableLeftClickListener(TextViewDrawableClick.OnDrawableLeftClickListener mListener) {
this.mLeftListener = mListener;
}
//相应的控件注册的事件
public void setOnDrawableRightClickListener(TextViewDrawableClick.OnDrawableRightClickListener mListener) {
this.mRightListener = mListener;
}
//相应的控件注册的事件
public void setOnDrawableTopClickListener(TextViewDrawableClick.OnDrawableTopClickListener mListener) {
this.mTopListener = mListener;
}
//相应的控件注册的事件
public void setOnDrawableBottomClickListener(TextViewDrawableClick.OnDrawableBottomClickListener mListener) {
this.mBottomListener = mListener;
}
//相应的控件注册的事件
public void setOnTextClickListener(TextViewDrawableClick.OnTextClickListener mListener){
this.mTextListener = mListener;
}
/**
* 回调接口需要,需要复写
*/
public interface OnDrawableRightClickListener {
void onDrawableRightClick(View view);
}
/**
* 回调接口
*/
public interface OnDrawableLeftClickListener {
void onDrawableLeftClick(View view);
}
/**
* 回调接口
*/
public interface OnDrawableTopClickListener {
void onDrawableTopClick(View view);
}
/**
* 回调接口
*/
public interface OnDrawableBottomClickListener {
void onDrawableBottomClick(View view);
}
/**
* 回调接口
*/
public interface OnTextClickListener{
void onTextClick(View view);
}
}
使用方法:
//在.xml文件中
<com.example.gaodemap.TextViewDrawableClick
android:id="@+id/history_name"
android:layout_width="match_parent"
android:layout_height="48dp"
android:paddingLeft="8dp"
android:drawableLeft="@drawable/history_record"
android:paddingRight="8dp"
android:drawableRight="@drawable/history_delete"
android:textSize="56px"
android:gravity="center"
android:drawablePadding="8dp"
android:clickable="true"/>//这一句非常重要,TextView默认不可点击
TextViewDrawableClick historyName = findViewById(R.id.history_name);
historyName.setText("我是文字");
historyName.setOnTextClickListener(new TextViewDrawableClick.OnTextClickListener() {
@Override
public void onTextClick(View view) {
Toast.makeText(mContext,"点击到文字了",Toast.LENGTH_SHORT).show();
}
});