Android:如何在 Edittext 中完全禁用复制和粘贴功能

Android: How to TOTALLY disable copy and paste function in Edittext(Android:如何在 Edittext 中完全禁用复制和粘贴功能)

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

问题描述

我对 Android 开发领域还很陌生,最近遇到了一个棘手的问题.

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

我正在尝试制作一个不应允许用户从中复制内容或将内容粘贴到其中的 Edittext.我用谷歌搜索了很多,发现似乎有两种流行的方法:

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 区域都只能被禁用长按,这会阻止用户通过长按访问全选,复制和粘贴"菜单.但是这两种解决方案都没有阻止用户通过简单地点击光标来访问粘贴"功能.

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 中完全禁用复制和粘贴功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android:如何在 Edittext 中完全禁用复制和粘贴功能

基础教程推荐