2016-01-29 118 views
2

我在这个问题上长时间寻找解决方案,所以我决定在这里问一下。如何禁用editText中的“复制/剪切/粘贴/”工具栏,但仍然有光标选择一些文本?

我写记事本应用程序,点击“新的注释”选项后,新的活动出现,看起来像这样:

NoteActivity screen with menu toolbar ON

因为我的应用程序的概念,它允许用户通过格式文本菜单编辑文本(在屏幕底部可见)我想让用户选择文本,而不显示底部的键盘和复制/剪切/粘贴菜单。键盘是通过切换按钮accesed(这工作得很好,对两行代码)

我已经当焦点是通过下面的代码设置的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); 
} 

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

NoteActivity with copy/cut/paste toolbar covering app toolbar

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

P.S.手机型号:HTC一个M7

任何帮助,将不胜感激

最好的问候, 汤姆

回答

1

对于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,菜单)返回false将阻止启动的动作模式(全选,剪切,复制和粘贴操作)。 最初是回答HERE

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

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

如需了解细节,请继续阅读。

解决方案在于防止PASTE/REPLACE菜单出现在(未记录的)android.widget.Editor类的show()方法中。在菜单出现之前,检查是否完成(!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

+1

我以前试过这个解决方案。它可以防止出现自定义上下文菜单,但也会禁用选择文本按钮。我有一个游标,但是我不能“分开这个游标”来选择开始和结束选择文本的节点。 – Tomek

+0

Yout下一个解决方案与第一个解决方案相同。我做了一些研究,并且发现,在手机处于水平方向模式时,实际上可以选择文本。当我在水平模式下打开键盘时,editText是我能看到的唯一的东西(键盘除外)。游标将应用程序默认的颜色更改为系统默认值,这真的很奇怪。我想我需要找到另一种解决方案,也许我只会为复制/粘贴工具栏留出空间。无论如何,感谢您的帮助,我真的很感谢:) – Tomek

+1

你是最好的。 – Stanojkovic

相关问题