How to disable paste in onClickListener for the Dr

2019-02-25 16:37发布

I have EditText and icon inside it

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

I Set onClickListener for the Drawable right of an 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;
        }
    });

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条回答
放荡不羁爱自由
2楼-- · 2019-02-25 17:05

In my case changing from ACTION_UP to ACTION_DOWN gave effect. Of course, behaviour sligtly changed. And cursor still jumps to the end of a text.

查看更多
走好不送
3楼-- · 2019-02-25 17:06

You can take a look at this question and see how to disable the paste.

But I'd suggest something else, instead of disabling paste, you can probably do this in a different way. Looking at whatsapp's microphone button + EditText placement, I notice that they're two different elements, and this is what you may do. Wrap your EditText in a horizontal LinearLayout and place a button with a microphone at the right end. I hope you can manage to make it work, and that you understand my idea.

查看更多
淡お忘
4楼-- · 2019-02-25 17:10

Just set android:longClickable="false" to editbox.

查看更多
登录 后发表回答