gpt4 book ai didi

android - 编辑文本 : Disable Paste/Replace menu pop-up on Text Selection Handler click event

转载 作者:IT王子 更新时间:2023-10-28 23:34:12 27 4
gpt4 key购买 nike

我的目标是拥有一个没有花哨功能的 EditText,只是用于更轻松地移动光标的文本选择处理程序——因此没有上下文菜单或弹出窗口。

根据 this solution,我通过使用 ActionMode 回调事件禁用了文本编辑功能操作栏的外观(复制/粘贴等)。 .

当字段中存在文本并且在文本中发生单击时,中间的中间文本选择句柄(见下图)仍会出现。伟大的!我想保持这种行为。我不希望在单击文本选择句柄时出现“粘贴”菜单。

Text selection handle with paste menu

我还通过在样式 XML 中设置 android:longClickable="false" 禁用了 EditText 的长按输入。禁用长按可防止在单击并按住鼠标(即长按)时出现“粘贴/替换”菜单,但是当在文本内单击鼠标(单击)时,将出现文本选择句柄,并且当单击文本选择句柄本身,然后出现“粘贴”菜单选项(当剪贴板中有文本时)。这是我试图阻止的。

从源代码中我可以看到,ActionPopupWindow 是 PASTE/REPLACE 选项弹出的内容。 ActionPopupWindow 是公共(public)类android.widget.Editor 中私有(private)抽象类HandleView 中的 protected 变量(mActionPopupWindow)...

没有禁用剪贴板服务或编辑 Android 源代码,有什么方法可以阻止它显示吗?我尝试为 android:textSelectHandleWindowStyle 定义一个新样式,并将 android:visibility 设置为 gone,但它不起作用(应用程序卡住一段时间,否则它会显示)。

最佳答案

解决方案:在 EditText 中覆盖 isSuggestionsEnabledcanPaste

为了快速解决,请复制下面的类 - 该类覆盖 EditText类,并相应地阻止所有事件。

有关坚韧不拔的细节,请继续阅读。

解决方案在于阻止 PASTE/REPLACE 菜单出现在 show() 中。 (未记录的)android.widget.Editor 类的方法。在菜单出现之前,会检查 if (!canPaste && !canSuggest) return;。作为设置这些变量的基础的两个方法都在 EditText 类中:

因此,将这些更新合并到一个也有 setCustomSelectionActionModeCallback 的类中,以及 disabled long-click ,这里是防止所有编辑(但仍显示 text selection handler )用于控制光标的完整类:

package com.cjbs.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;


/**
* This is a thin veneer over EditText, with copy/paste/spell-check removed.
*/
public class NoMenuEditText extends EditText
{
private final Context context;

/** This is a replacement method for the base TextView class' method of the same name. This
* method is used in hidden class android.widget.Editor to determine whether the PASTE/REPLACE popup
* appears when triggered from the text insertion handle. Returning false forces this window
* to never appear.
* @return false
*/
boolean canPaste()
{
return false;
}

/** This is a replacement method for the base TextView class' method of the same name. This method
* is used in hidden class android.widget.Editor to determine whether the PASTE/REPLACE popup
* appears when triggered from the text insertion handle. Returning false forces this window
* to never appear.
* @return false
*/
@Override
public boolean isSuggestionsEnabled()
{
return false;
}

public NoMenuEditText(Context context)
{
super(context);
this.context = context;
init();
}

public NoMenuEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
this.context = context;
init();
}

public NoMenuEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
this.context = context;
init();
}

private void init()
{
this.setCustomSelectionActionModeCallback(new ActionModeCallbackInterceptor());
this.setLongClickable(false);
}


/**
* Prevents the action bar (top horizontal bar with cut, copy, paste, etc.) from appearing
* by intercepting the callback that would cause it to be created, and returning false.
*/
private class ActionModeCallbackInterceptor implements ActionMode.Callback
{
private final String TAG = NoMenuEditText.class.getSimpleName();

public boolean onCreateActionMode(ActionMode mode, Menu menu) { return false; }
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; }
public void onDestroyActionMode(ActionMode mode) {}
}
}

我已经在 Android v4.4.2 和 v4.4.3 中对此进行了测试。

关于android - 编辑文本 : Disable Paste/Replace menu pop-up on Text Selection Handler click event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27869983/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com