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 中完全禁用复制和粘贴功能
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
