Android的:如何清除右侧一个EditText交叉按钮 [英] Android:how to clear an edittext by cross button in the right side

查看:226
本文介绍了Android的:如何清除右侧一个EditText交叉按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个EditText搜索,其中包含在左侧搜索图标,并在右侧十字图标:

I have created an edittext for search, which contains in the left side a search icon and in the right side a cross icon:

<EditText
    android:id="@+id/Search"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:drawableLeft="@android:drawable/ic_menu_search"
    android:drawableRight="@android:drawable/ic_delete"
    android:hint="Search Product .." >
</EditText>

我想知道我怎么可以清除EDITTEXT的内容时,我点击十字按钮。 谢谢你在前进。

I want to know how can i clear the content of editText when I click in the cross button. Thank you in advance.

推荐答案

从@aristo_sh的改进的答案<一href="http://stackoverflow.com/questions/3554377/handling-click-events-on-a-drawable-within-an-edittext">Handling点击在一个EditText

An improved answer by @aristo_sh from Handling click events on a drawable within an EditText

    mQueryEditText.setOnTouchListener(new OnTouchListener() {
        final int DRAWABLE_LEFT = 0;
        final int DRAWABLE_TOP = 1;
        final int DRAWABLE_RIGHT = 2;
        final int DRAWABLE_BOTTOM = 3;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                int leftEdgeOfRightDrawable = mQueryEditText.getRight() 
                      - mQueryEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width();
                // when EditBox has padding, adjust leftEdge like
                // leftEdgeOfRightDrawable -= getResources().getDimension(R.dimen.edittext_padding_left_right);
                if (event.getRawX() >= leftEdgeOfRightDrawable) {
                    // clicked on clear icon
                    mQueryEditText.setText("");
                    return true;
                }
            }
            return false;
        }
    });

这篇关于Android的:如何清除右侧一个EditText交叉按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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