需要自定義EditText 並重寫裏面的 onTextContextMenuItem 方法:
package myapp.first.myapplication.view; import android.content.Context; import android.util.AttributeSet; import android.widget.EditText; /** * Created by Administrator on 2016/10/11 0011. */ public class MyEditText extends EditText { public MyEditText(Context context) { super(context); } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTextContextMenuItem(int id) { // id:16908319 --- 全選 // id:16908328 --- 選擇 // id:16908320 --- 剪貼 // id:16908321 --- 複製 // id:16908322 --- 粘貼 // id:16908324 --- 輸入法 return super.onTextContextMenuItem(id); } }
詳細介紹: ----- 請看源碼中的英文註釋
這些都被用id給賦值了 並且靜態最終
這些id值點擊是看不到的 需要 按ctrl 然後鼠標放到上面 可查看具體的 id值
static final int ID_SELECT_ALL = android.R.id.selectAll; static final int ID_UNDO = android.R.id.undo; static final int ID_REDO = android.R.id.redo; static final int ID_CUT = android.R.id.cut; static final int ID_COPY = android.R.id.copy; static final int ID_PASTE = android.R.id.paste; static final int ID_SHARE = android.R.id.shareText; static final int ID_PASTE_AS_PLAIN_TEXT = android.R.id.pasteAsPlainText; static final int ID_REPLACE = android.R.id.replaceText; /** * Called when a context menu option for the text view is selected. Currently * this will be one of {@link android.R.id#selectAll}, {@link android.R.id#cut}, * {@link android.R.id#copy}, {@link android.R.id#paste} or {@link android.R.id#shareText}. * * @return true if the context menu item action was performed. */ public boolean onTextContextMenuItem(int id) { int min = 0; int max = mText.length(); if (isFocused()) { final int selStart = getSelectionStart(); final int selEnd = getSelectionEnd(); min = Math.max(0, Math.min(selStart, selEnd)); max = Math.max(0, Math.max(selStart, selEnd)); } switch (id) { case ID_SELECT_ALL: selectAllText(); return true; case ID_UNDO: if (mEditor != null) { mEditor.undo(); } return true; // Returns true even if nothing was undone. case ID_REDO: if (mEditor != null) { mEditor.redo(); } return true; // Returns true even if nothing was undone. case ID_PASTE: paste(min, max, true /* withFormatting */); return true; case ID_PASTE_AS_PLAIN_TEXT: paste(min, max, false /* withFormatting */); return true; case ID_CUT: setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max))); deleteText_internal(min, max); return true; case ID_COPY: setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max))); stopTextActionMode(); return true; case ID_REPLACE: if (mEditor != null) { mEditor.replace(); } return true; case ID_SHARE: shareSelectedText(); return true; } return false; }
當我們複製了 一段帶有表情的文本內容 粘貼到 edittext 上面時 表情的圖標不會顯示 會顯示的是 表情的編碼
解決:
可給自定義的EditText加個 監聽回調 或者加個屬性 在重寫的方法中去記錄這個 id值
注意: 別用id這個字段 因爲 系統的所有控件中 都存在 id這個屬性 可以換個別的名字記錄
監聽自定義EditText的 文本改變事件
在事件中 根據 記錄的 id值 判斷執行相應的操作 (把複製的帶表情編碼的字符串進行編碼轉換 然後再設置到EditText)
這樣就可以實現賦值後的內容展示爲 帶標籤圖標到輸入框中