How to disable copy/paste from/to EditText

后端 未结 24 1311
情歌与酒
情歌与酒 2020-11-22 12:18

In my application, there is a registration screen, where i do not want the user to be able to copy/paste text into the EditText field. I have set an onLon

相关标签:
24条回答
  • 2020-11-22 12:55

    I am able to disable copy-and-paste functionality with the following:

    textField.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
    
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }
    
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }
    
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem item) {
            return false;
        }
    
        public void onDestroyActionMode(ActionMode actionMode) {
        }
    });
    
    textField.setLongClickable(false);
    textField.setTextIsSelectable(false);
    

    Hope it works for you ;-)

    0 讨论(0)
  • 2020-11-22 12:56

    @Zain Ali, your answer works on API 11. I just wanted to suggest a way to do in on API 10 as well. Since I had to maintain my project API on that version, I was constantly playing with the functions available in 2.3.3 and got a possibility to do it. I have share the snippet below. I tested the code and it was working for me. I did this snippet on an urgency. Feel free to improve the code if there are any changes that can be done..

    // A custom TouchListener is being implemented which will clear out the focus 
    // and gain the focus for the EditText, in few milliseconds so the selection 
    // will be cleared and hence the copy paste option wil not pop up.
    // the respective EditText should be set with this listener 
    // tmpEditText.setOnTouchListener(new MyTouchListener(tmpEditText, tmpImm));
    
    public class MyTouchListener implements View.OnTouchListener {
    
        long click = 0;
        EditText mEtView;
        InputMethodManager imm;
    
        public MyTouchListener(EditText etView, InputMethodManager im) {
            mEtView = etView;
            imm = im;
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
    
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                long curr = System.currentTimeMillis();
                if (click !=0 && ( curr - click) < 30) {
    
                    mEtView.setSelected(false);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mEtView.setSelected(true);
                            mEtView.requestFocusFromTouch();
                            imm.showSoftInput(mEtView, InputMethodManager.RESULT_SHOWN);
                        }
                    },25);
    
                return true;
                }
                else {
                    if (click == 0)
                        click = curr;
                    else
                        click = 0;
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mEtView.requestFocusFromTouch();
                            mEtView.requestFocusFromTouch();
                            imm.showSoftInput(mEtView, InputMethodManager.RESULT_SHOWN);
                        }
                    },25);
                return true;
                }
    
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                mEtView.setSelected(false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mEtView.setSelected(true);
                        mEtView.requestFocusFromTouch();
                        mEtView.requestFocusFromTouch();
                        imm.showSoftInput(mEtView, InputMethodManager.RESULT_SHOWN);
                    }
                },25);
                return true;
            }
            return false;
        }
    
    0 讨论(0)
  • 2020-11-22 12:58

    If you don't wan't to disable long click because you need to perform some functionality on long click than returning true is a better option to do so.

    Your edittext long click will be like this.

    edittext.setOnLongClickListener(new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View v) {
                //  Do Something or Don't
                return true;
          }
    });
    

    As per documentation Returning "True" will indicate that long click have been handled so no need to perform default operations.

    I tested this on API level 16, 22 and 25. Its working fine for me. Hope this will help.

    0 讨论(0)
  • 2020-11-22 12:58

    Here is a hack to disable "paste" popup. You have to override EditText method:

    @Override
    public int getSelectionStart() {
        for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
            if (element.getMethodName().equals("canPaste")) {
                return -1;
            }
        }
        return super.getSelectionStart();
    }
    

    Similar can be done for the other actions.

    0 讨论(0)
  • 2020-11-22 12:59

    If you want to disable ActionMode for copy/pasting, you need to override 2 callbacks. This works for both TextView and EditText (or TextInputEditText)

    import android.view.ActionMode
    
    fun TextView.disableCopyPaste() {
      isLongClickable = false
      setTextIsSelectable(false)
      customSelectionActionModeCallback = object : ActionMode.Callback {
        override fun onCreateActionMode(mode: ActionMode?, menu: Menu) = false
        override fun onPrepareActionMode(mode: ActionMode?, menu: Menu) = false
        override fun onActionItemClicked(mode: ActionMode?, item: MenuItem) = false
        override fun onDestroyActionMode(mode: ActionMode?) {}
      }
      //disable action mode when edittext gain focus at first
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        customInsertionActionModeCallback = object : ActionMode.Callback {
          override fun onCreateActionMode(mode: ActionMode?, menu: Menu) = false
          override fun onPrepareActionMode(mode: ActionMode?, menu: Menu) = false
          override fun onActionItemClicked(mode: ActionMode?, item: MenuItem) = false
          override fun onDestroyActionMode(mode: ActionMode?) {}
        }
      }
    }
    

    This extension is based off above @Alexandr solution and worked fine for me.

    0 讨论(0)
  • 2020-11-22 13:02

    Best method is to use:

    etUsername.setLongClickable(false);
    
    0 讨论(0)
提交回复
热议问题