android 禁用剪切板,Android:如何完全禁用Edittext中的复制和粘贴功能

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

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;

}

});

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.

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值