的EditText:禁止粘贴/替换菜单弹出的文本选择处理程序click事件 [英] EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event

查看:1562
本文介绍了的EditText:禁止粘贴/替换菜单弹出的文本选择处理程序click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是有一个的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粘贴/更换菜单的出现在<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/widget/Editor.java#Editor.ActionPopupWindow.show%28%29"><$c$c>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:

  • <一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/widget/TextView.java#TextView.isSuggestionsEnabled%28%29"><$c$c>isSuggestionsEnabled()是<一个href="http://developer.android.com/reference/android/widget/TextView.html#isSuggestionsEnabled()">public,和可因此被覆盖。
  • <一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/widget/TextView.java#TextView.canPaste%28%29"><$c$c>canPaste()不是,因此必须被隐藏<​​a href="http://stackoverflow.com/a/2000156/3063884">推出的同名的函数在派生类中。
  • 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 和的disabled长单击,这里是满级,以prevent所有编辑(但仍显示的文本选择处理程序):

So incorporating these updates into a class that also has the setCustomSelectionActionModeCallback, and the disabled long-click, here is the full class to prevent all editing (but still display the text selection handler) for controlling the cursor:

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

我在的Andr​​oid v4.4.2测试这一点。

I've tested this in Android v4.4.2.

这篇关于的EditText:禁止粘贴/替换菜单弹出的文本选择处理程序click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆