如何添加OnClickListener中的EditText来绘制的? [英] How can I add OnClickListener to Drawable in EditText?

查看:312
本文介绍了如何添加OnClickListener中的EditText来绘制的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的EditText和要添加到右侧的搜索图标..

I have edittext and want to add to right "search" icon..

searchTxt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.search, 0);

但我怎么可以添加事件单击此图标?

But how can I add event for click this icon?

searchTxt.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
    Drawable co = ((TextView) v).getCompoundDrawables()[2];

    if (event.getX() > v.getMeasuredWidth() - v.getPaddingRight()
                     - co.getIntrinsicWidth()) {
        Datas.search = searchTxt.getText().toString();
            startActivity(Search.class);
        return true;
    } else {
        return false;
    }
    }

});

...

推荐答案

可绘制是非互动元素,让你cannont设置的点击监听器上绘制。
一个简单的解决办法是把一个的ImageView包含搜索在图标的EditText 并添加上一个右填充的EditText ,使文本不与图标重叠。

Drawables are non-interactive elements, so you cannont set a click listener on a drawable. A simple solution would be putting an ImageView containing the "search" icon over the EditText and adding a right padding on the EditText so the text doesn't overlap with the icon.

<FrameLayout android:layout_width="wrap_content"
             android:layout_height="wrap_content">
    <EditText ...
              android:paddingRight="..."
              ... />
    <ImageView ...
               android:src="@drawable/search"
               android:layout_gravity="right|center_vertical"
               ... />
</FrameLayout>

这篇关于如何添加OnClickListener中的EditText来绘制的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆