Android: How to TOTALLY disable copy and paste function in 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 中完全禁用复制和粘贴功能
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01