如何禁用从/到 EditText 的复制/粘贴 [英] How to disable copy/paste from/to EditText

查看:40
本文介绍了如何禁用从/到 EditText 的复制/粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,有一个注册屏幕,我不希望用户能够将文本复制/粘贴到 EditText 字段中.我在每个 EditText 上设置了一个 onLongClickListener,这样显示复制/粘贴/输入方法和其他选项的上下文菜单就不会出现.因此用户将无法复制/粘贴到编辑字段中.

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 onLongClickListener on each EditText so that the context menu showing copy/paste/inputmethod and other options does not show up. So the user won't be able to copy/ paste into the Edit fields.

 OnLongClickListener mOnLongClickListener = new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // prevent context menu from being popped up, so that user
            // cannot copy/paste from/into any EditText fields.
            return true;
        }
    };

但如果用户启用了非 Android 默认键盘的第三方键盘,则问题就会出现,该键盘可能有一个用于复制/粘贴的按钮,或者可能显示相同的上下文菜单.那么如何在这种情况下禁用复制/粘贴?

But the problem arises if the user has enabled a third-party keyboard other than the Android default, which may have a button to copy/paste or which may show the same context menu. So how do i disable copy/paste in that scenario ?

如果还有其他方法可以复制/粘贴,请告诉我.(以及可能如何禁用它们)

Please let me know if there are other ways to copy/paste as well. (and possibly how to disable them)

任何帮助将不胜感激.

推荐答案

如果您使用的是 API 级别 11 或更高级别,那么您可以阻止复制、粘贴、剪切和自定义上下文菜单出现.

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;
            }
        });

从 onCreateActionMode(ActionMode, Menu) 返回 false 将阻止启动操作模式(全选、剪切、复制和粘贴操作).

这篇关于如何禁用从/到 EditText 的复制/粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆