的EditText:禁止粘贴/替换菜单弹出的文本选择处理程序click事件弹出、菜单、文本、事件

2023-09-12 03:36:10 作者:一个人一座城一生的心疼

我的目标是有一个的EditText ,有没有花哨的功能,只是选择的文本处理程序更容易移动光标 - 因此没有上下文菜单或弹出窗口

My goal is to have an EditText that has no fancy features, just the Text Selection Handler for moving the cursor more easily -- so no context menus or pop-ups.

我已禁用的文本编辑功能动作条的外观(复制/粘贴等),通过消耗ActionMode回调事件,按的这个解决方案。

I've disabled the appearance of the text editing function actionbar (copy/Paste etc.) by consuming the ActionMode Callback event, as per this solution.

中间的中间文本选择手柄(见下图)时仍然在该领域存在的文本,并点击出现在文本中出现。大!我想保持这种行为。我不想要的是粘贴菜单中的文本选择手柄本身被点击时出现。

The middle Middle Text Select Handle (see image below) still appears when text exists in the field and a click occurs within the text. Great! I want to keep this behaviour. What I DON'T want is the "PASTE" menu to appear when the Text Select Handle itself is clicked.

我也被禁止长时间点击输入的EditText上通过设置安卓longClickable =假的样式的XML。禁用粘贴/替换从鼠标点击,并举行时出现的菜单(即长触摸),但是鼠标点击文本中(单点触摸)时,显示的文本选择柄的长按prevents,与文本选择手柄本身被点击时,再粘贴菜单项出现(当有在剪贴板文本)。这就是我想要prevent。

I have also disabled long-click input for the EditText by setting android:longClickable="false" in the styles XML. Disabling the long click prevents the "Paste/Replace" menu from appearing when the mouse is clicked and held (i.e. long touch), however when the mouse is clicked (single touch) within the text, the text selection handle appears, and when the text selection handle itself is clicked, then the "paste" menu option appears (when there's text in the clipboard). This is what I'm trying to prevent.

从我可以从源头上看到的, ActionPopupWindow 是弹出与粘贴/替换功能。 ActionPopupWindow是一个受保护的变量(mActionPopupWindow)私人抽象类HandleView公共类中android.widget.Editor ...

From what I can see from the source, the ActionPopupWindow is what pops up with the PASTE/REPLACE options. ActionPopupWindow is a protected variable (mActionPopupWindow) in the private abstract class HandleView within public class android.widget.Editor...

禁用剪贴板的服务或编辑Android源$ C ​​$ C的简称,是没有办法,我可以prevent这个从显示的方式?我试图定义一个新的样式安卓textSelectHandleWindowStyle ,并设置机器人:能见度 gone`,但它没科技工作(应用程序愣了,而将本来显示)。

Short of disabling the clipboard service or editing the Android Source code, is there a way that I can prevent this from showing? I tried to define a new style for android:textSelectHandleWindowStyle, and set android:visibilitytogone`, but it didn't work (app froze for a while when it would otherwise have shown).

推荐答案

解决方案:覆盖 isSuggestionsEnabled canPaste 的EditText

Solution: Override isSuggestionsEnabled and canPaste in EditText.

有关的快速解决方案,复制下面的类 - 这个类覆盖 的EditText 类,并阻止所有事件相应。

For the quick solution, copy the class below - this class overrides the EditText class, and blocks all events accordingly.

有关的相关细节,请继续阅读。

For the gritty details, keep reading.

解决之道在于preventing粘贴/更换菜单的出现在show() (非记录) android.widget.Editor 类的方法。在菜单中出现,一检查,以如果(canPaste&安培;!&安培;!canSuggest)回报; 。被作为基础来设置这些变量的两种方法都在的EditText 类:

The solution lies in preventing PASTE/REPLACE menu from appearing in the show() method of the (non-documented) android.widget.Editor class. Before the menu appears, a check is done to if (!canPaste && !canSuggest) return;. The two methods that are used as the basis to set these variables are both in the EditText class:

isSuggestionsEnabled()是public,和可因此被覆盖。 canPaste()不是,因此必须被隐藏推出的同名的函数在派生类中。 isSuggestionsEnabled() is public, and may thus be overridden. canPaste() is not, and thus must be hidden by introducing a function of the same name in the derived class.

所以将这些更新为一类,也有 setCustomSelectionActionModeCallback 和的