How to disable copy/paste from/to EditText

后端 未结 24 1353
情歌与酒
情歌与酒 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:52

    Solution that worked for me was to create custom Edittext and override following method:

    public class MyEditText extends EditText {
    
    private int mPreviousCursorPosition;
    
    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        CharSequence text = getText();
        if (text != null) {
            if (selStart != selEnd) {
                setSelection(mPreviousCursorPosition, mPreviousCursorPosition);
                return;
            }
        }
        mPreviousCursorPosition = selStart;
        super.onSelectionChanged(selStart, selEnd);
    }
    

    }

提交回复
热议问题