2016-01-29 121 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

相關問題