监听EditText的复制、粘贴、全选、剪切、选择等状态

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

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值