需要自定义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)这样就可以实现赋值后的内容展示为 带标签图标到输入框中