处理EditText中drawable上的单击事件我EditText使用以下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点击嵌入图像的时间。我怎样才能做到这一点?
3 回答
LEATH
TA贡献1936条经验 获得超6个赞
非常非常好,感谢所有为此次讨论做出贡献的人。因此,如果您不想处理扩展课程的不便,您可以执行以下操作(仅适用于正确的绘图)
this.keyword = (AutoCompleteTextView) findViewById(R.id.search);this.keyword.setOnTouchListener(new RightDrawableOnTouchListener(keyword) { @Override public boolean onDrawableTouch(final MotionEvent event) { return onClickSearch(keyword,event); } });private boolean onClickSearch(final View view, MotionEvent event) { // do something event.setAction(MotionEvent.ACTION_CANCEL); return false;}
这是基于@Mark答案的裸骨听众实现
public abstract class RightDrawableOnTouchListener implements OnTouchListener { Drawable drawable; private int fuzz = 10; /** * @param keyword */ public RightDrawableOnTouchListener(TextView view) { super(); final Drawable[] drawables = view.getCompoundDrawables(); if (drawables != null && drawables.length == 4) this.drawable = drawables[2]; } /* * (non-Javadoc) * * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent) */ @Override public boolean onTouch(final View v, final MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN && drawable != null) { final int x = (int) event.getX(); final int y = (int) event.getY(); final Rect bounds = drawable.getBounds(); if (x >= (v.getRight() - bounds.width() - fuzz) && x <= (v.getRight() - v.getPaddingRight() + fuzz) && y >= (v.getPaddingTop() - fuzz) && y <= (v.getHeight() - v.getPaddingBottom()) + fuzz) { return onDrawableTouch(event); } } return false; } public abstract boolean onDrawableTouch(final MotionEvent event);}
- 3 回答
- 0 关注
- 559 浏览
添加回答
举报
0/150
提交
取消