How to disable copy/paste from/to EditText

后端 未结 24 1802
情歌与酒
情歌与酒 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: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;
        }
    

提交回复
热议问题