How to disable copy/paste from/to EditText

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

    If you are using API level 11 or above then you can stop copy,paste,cut and custom context menus from appearing by.

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

    Returning false from onCreateActionMode(ActionMode, Menu) will prevent the action mode from being started(Select All, Cut, Copy and Paste actions).

提交回复
热议问题