Android:如何完全禁用Edittext中的复制和粘贴功能 [英] Android: How to TOTALLY disable copy and paste function in Edittext

查看:2584
本文介绍了Android:如何完全禁用Edittext中的复制和粘贴功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发领域的新手,最近我遇到了一个棘手的问题.

I am quite new to Android developing area and recently I hv encountered a tough problem.

我试图制作一个Edittext,该文本不应允许用户从中复制内容或将内容粘贴到其中.我在Google上搜索了很多,发现这样做似乎有两种流行的方式:

I was trying to make a Edittext which should NOT ALLOW user to copy content from or paste content to it. I hv googled a lot and find there seems to be 2 popular ways of doing so:

第一种方法,将其设置在布局文件中:

1st way, to set it in the layout file:

android:longClickable="false"

第二种方式,以编程方式设置它:

2nd way, to programmatically set it:

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

但是我发现无论选择哪种方式,都只能长时间单击禁用edittext区域,这将阻止用户通过长时间单击来访问全选,复制和粘贴"菜单.但是两种解决方案DID都不会阻止用户仅通过轻按光标即可访问粘贴"功能.

But I just found that whichever way I chose, the edittext area could only be disabled from long clickable, which then prevents user from accessing the "select all, copy and paste" menu through long clicking. But both the 2 solution DID NOT prevent the user from accessing the "paste" function through just a simple tap on the cursor.

所以我的问题是:我怎样才能完全阻止用户使用某个Edittext中的复制和粘贴功能.有人帮忙吗?非常感谢

So my question is: how could I TOTALLY block user from copy and paste function in a certain Edittext. Is anyone help? Thx a lot

推荐答案

有一种可能是通过禁用游标处理程序.您将不会获得粘贴按钮,但也将无法通过触摸来移动光标.

There is one possibility, by disabling the cursor handler. You won't get the paste button, but you will also not be able to move the cursor with touch.

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getActionMasked() == MotionEvent.ACTION_UP && mDisableCursorHandle) {
        // Hack to prevent keyboard and insertion handle from showing.
        cancelLongPress();
    }
    return super.onTouchEvent(event);
}

这篇关于Android:如何完全禁用Edittext中的复制和粘贴功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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