如何为Android的EditText的Drawable权限禁用onClickListener中的粘贴(图标EditText内部) [英] How to disable paste in onClickListener for the Drawable right of an EditText Android (inside icon EditText)

查看:80
本文介绍了如何为Android的EditText的Drawable权限禁用onClickListener中的粘贴(图标EditText内部)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我里面有EditText和图标

<EditText
    android:id="@+id/myedittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableRight="@mipmap/microphone"/>

我在EditText

myeditText.setOnTouchListener(new View.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() >= (myeditText.getRight() - myeditText
      .getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {  
                    // your action here
                    Toast.makeText(getApplicationContext(),
                            "speak",Toast.LENGTH_SHORT).show();
                    return true;
                }
            }
            return false;
        }
    });

当我单击EditText右侧的图标时,Toast向我展示并工作,但向我展示EditText上的粘贴选项. 右键单击图标时如何删除粘贴?

when I click to icon right of EditText Toast show me and work, but show me paste option on EditText too. how can I remove paste when icon in right clicked?

推荐答案

嗯,已经有3年了.但是,对于像我这样的人可能会面临这个挑战:解决方案是在ACTION_DOWN中也返回true,但仅在ACTION_DOWN上调用侦听器,并且也返回true.在这种情况下,您先检查单击的区域是否与图标对应,然后再继续向上或向下检查操作 像这样:

Well, it's already about 3 years. However, for someone like me that might face this challenge: The solution is to also return true in ACTION_DOWN but only call the listener on ACTION_DOWN, returning true as well. In this case, you first check the region clicked corresponds with that of the icon, before proceeding to check action up or down Something like:

@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.getRawX() >= (myeditText.getRight() - myeditText
                .getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                // your action here
                listener.onClick(v);
                return true;
            }

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                return true;
            }
        }

        return false;
    }

这篇关于如何为Android的EditText的Drawable权限禁用onClickListener中的粘贴(图标EditText内部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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