[英]Android - Detecting when user touches a drawable left
I have a TextView
that contains a DrawableRight
, what I want to do is detecting when the user presses that icon in drawableRight, is that possible ? 我有一个包含DrawableRight
的TextView
,我想做的就是检测用户何时在drawableRight中按下该图标,这可能吗? and if it is how can I do it ? 如果是我该怎么办?
PS: I am working inside a fragment PS:我正在处理一个片段
TextView XML 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
/>
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;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.