禁用Java/Kotlin中的EditText剪贴板操作



如何在Java/Kotlin代码中禁用对密码类型字段的文本复制和粘贴操作?

如果您使用的是API 11级或更高级别,则可以停止复制、粘贴、剪切和自定义上下文菜单。

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

从onCreateActionMode(ActionMode,Menu(返回false将阻止启动操作模式(全选、剪切、复制和粘贴操作(。

fun disableClipboardAction(context : Context, editText: EditText){
try {
editText.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
val clipboard: ClipboardManager =
context.getSystemService(AppCompatActivity.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText("text", "")
clipboard.setPrimaryClip(clipData)
}
}

editText.customSelectionActionModeCallback =
object : android.view.ActionMode.Callback {
override fun onCreateActionMode(
mode: android.view.ActionMode?,
menu: Menu?
): Boolean {
return false
}

override fun onPrepareActionMode(
mode: android.view.ActionMode?,
menu: Menu?
): Boolean {
return false
}

override fun onActionItemClicked(
mode: android.view.ActionMode?,
item: MenuItem?
): Boolean {
return false
}

override fun onDestroyActionMode(mode: android.view.ActionMode?) {
}

}
} catch (exception: Exception) {
exception.printStackTrace()
}
}

相关内容

  • 没有找到相关文章

最新更新