how to disable paste option in android EditText

2019-02-18 14:32发布

I have an requirement where EditText doesn't allow paste but it should allow copy. I tried setCustomSelectionActionModeCallback but it disable copy option.

EditText  etxt = (EditText) findViewById(R.id.editText1);
etxt.setCustomSelectionActionModeCallback(new 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;
            }
        });

3条回答
不美不萌又怎样
2楼-- · 2019-02-18 14:49

If you want to disable only the paste option, I think it is better to remove the paste context menu item, this is a util method I implemented in my apps to achieve that (I also removed cut item)

    public static void toggleContextMenuType(EditText editText) {

    if (editText.getCustomSelectionActionModeCallback() == null) {
        editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                menu.removeItem(16908322);
                menu.removeItem(16908320);
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
            }
        });
    }else{
        editText.setCustomSelectionActionModeCallback(null);
    }
}
查看更多
Anthone
3楼-- · 2019-02-18 14:54

I done my self..credit goes to Android intercept paste\copy\cut on editText

I just changed onTextContextMenuItem() based on my requirement..

@Override
    public boolean onTextContextMenuItem(int id) {
        // Do your thing:
        //boolean consumed = super.onTextContextMenuItem(id); // Change1
        // React:
        switch (id){
            case android.R.id.cut:
                onTextCut();
                break;
            case android.R.id.paste:
                onTextPaste();
                return false;       //Change2
                //break;
            case android.R.id.copy:
                onTextCopy();
        }
        return true; // Change3
    }
查看更多
Melony?
4楼-- · 2019-02-18 14:55

If you are using API level 11 or above use this

      edittext.setCustomSelectionActionModeCallback(new 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;
        }
    });
查看更多
登录 后发表回答