Android - 检测用户何时触及可绘制的左侧

时间:2016-12-08 14:45:59

标签: java android drawable touch-event

我有TextView包含DrawableRight,我想要做的是检测用户何时在drawableRight中按下该图标,这可能吗?如果是我该怎么办?

PS:我在片段中工作

TextView XML

<TextView
    android:id="@+id/mTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textIsSelectable="false"
    android:textSize="22dp"         
    android:drawableRight="@mipmap/icn" //this is the drawable
/>

1 个答案:

答案 0 :(得分:0)

mTitle.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
   final int DRAWABLE_RIGHT = 2;
   if (event.getAction() == MotionEvent.ACTION_UP) {
   if (event.getRawX() >= (mTitle.getRight() - mTitle.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
    //drawable pressed
     return true;
        }
       }
      return false;
      }
    });