如何禁用editText中的“复制/剪切/粘贴/”工具栏,但仍然有光标选择某些文本?

2024-01-07

我长期以来一直在寻找这个问题的解决方案,所以我决定在这里提问。

我正在编写记事本应用程序,单击“新笔记”选项后,会出现新的活动,如下所示:

由于我的应用程序概念允许用户通过格式文本菜单(在屏幕底部可见)编辑文本,我希望允许用户选择文本,而不显示键盘底部和复制/剪切/粘贴菜单。键盘可通过切换按钮访问(这在 2 行代码上效果很好)

当焦点通过以下代码设置在 EditText 上时,我已禁用键盘弹出窗口:

public static void disableSoftInputFromAppearing(EditText editText) {
    if (Build.VERSION.SDK_INT >= 11) {
        editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
        editText.setTextIsSelectable(true);
    } else {
        editText.setRawInputType(InputType.TYPE_NULL);
        editText.setFocusable(true);
    }
}

应用程序工具栏上的键盘按钮调用的方法:

public void toggleKeyboard(MenuItem item) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}

我想选择文本(通过双击文本或长按文本来实现),但主要问题是复制/剪切/粘贴工具栏,因为它覆盖了我的应用程序工具栏:

这就是为什么我想摆脱这个工具栏,但仍然可以选择特定的文本范围。

附:手机型号:HTC One M7

任何帮助,将不胜感激

此致, 汤姆


对于 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 将阻止启动操作模式(全选、剪切、复制和粘贴操作)。 最初回答HERE https://stackoverflow.com/questions/6275299/how-to-disable-copy-paste-from-to-edittext

解决方案:在 EditText 中覆盖 isSuggestionsEnabled 和 canPaste。

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

有关详细信息,请继续阅读。

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

isSuggestionsEnabled()是公开的,因此可以被覆盖。canPaste()不是,因此必须通过在派生类中引入同名函数来隐藏。 因此,将这些更新合并到一个类中,该类也具有setCustomSelectionActionModeCallback,以及禁用的长按,这里是阻止所有编辑(但仍显示文本选择处理程序)来控制光标的完整类:

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) {}
    }
} 

最初回答HERE2 https://stackoverflow.com/questions/27869983/edittext-disable-paste-replace-menu-pop-up-on-text-selection-handler-click-even

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何禁用editText中的“复制/剪切/粘贴/”工具栏,但仍然有光标选择某些文本? 的相关文章

随机推荐