本文介紹了處理 EditText 中可繪制對象的點擊事件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我使用以下 XML 在 EditText
小部件中添加了文本右側的圖像:
I have added an image right of the text in an EditText
widget, using the following XML:
<EditText
android:id="@+id/txtsearch"
...
android:layout_gravity="center_vertical"
android:background="@layout/shape"
android:hint="Enter place,city,state"
android:drawableRight="@drawable/cross" />
但我想在單擊嵌入圖像時清除 EditText
.我該怎么做?
But I want to clear the EditText
when the embedded image is clicked. How can I do this?
推薦答案
其實你不需要擴展任何類.假設我有一個帶有 drawableRight 的 EditText editComment
Actually you don't need to extend any class. Let's say I have an EditText editComment with a drawableRight
editComment.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
return true;
}
}
return false;
}
});
我們 getRawX()
因為我們想要獲取屏幕上觸摸的實際位置,而不是相對于父級.
we getRawX()
because we want to get the actual position of touch on screen, not relative to parent.
左鍵點擊
if(event.getRawX() <= (editComment.getCompoundDrawables()[DRAWABLE_LEFT].getBounds().width()))
這篇關于處理 EditText 中可繪制對象的點擊事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!