Android 自定义EditText实现粘贴,复制,剪切的监听

package com.dwtedx.qq.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

/**
 * An EditText, which notifies when something was cut/copied/pasted inside it.
 * 
 * @author Lukas Knuth
 * @version 1.0
 */
@SuppressLint("NewApi") public class MyEditText extends EditText implements
        MenuItem.OnMenuItemClickListener {
    private static final int ID_SELECTION_MODE = android.R.id.selectTextMode;
      // Selection context mode
    private static final int ID_SELECT_ALL = android.R.id.selectAll;
    private static final int ID_CUT = android.R.id.cut;
    private static final int ID_COPY = android.R.id.copy;
    private static final int ID_PASTE = android.R.id.paste;
    
    private final Context mContext;

    /*
     * Just the constructors to create a new EditText...
     */
    public MyEditText(Context context) {
        super(context);
        this.mContext = context;
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
    }

    @Override
    protected void onCreateContextMenu(ContextMenu menu) {
    //代码效果,有弹出框选择 粘贴,复制,剪切,类似qq效果..... menu.add(
0, ID_PASTE, 0, "粘贴").setOnMenuItemClickListener(this); menu.add(0, ID_CUT, 1, "剪切").setOnMenuItemClickListener(this); menu.add(0, ID_COPY, 1, "复制").setOnMenuItemClickListener(this); menu.add(0, ID_SELECT_ALL, 1, "全选").setOnMenuItemClickListener(this); super.onCreateContextMenu(menu); } @Override public boolean onMenuItemClick(MenuItem item) { // TODO Auto-generated method stub return onTextContextMenuItem(item.getItemId()); } @Override public boolean onTextContextMenuItem(int id) { // Do your thing: boolean consumed = super.onTextContextMenuItem(id); // React: switch (id) { case android.R.id.cut: onTextCut(); break; case android.R.id.paste: onTextPaste(); break; case android.R.id.copy: onTextCopy(); } return consumed; } /** * Text was cut from this EditText. */ public void onTextCut() { Toast.makeText(mContext, "Cut!", Toast.LENGTH_SHORT).show(); } /** * Text was copied from this EditText. */ public void onTextCopy() { Toast.makeText(mContext, "Copy!", Toast.LENGTH_SHORT).show(); } /** * Text was pasted into the EditText. */ public void onTextPaste() { Toast.makeText(mContext, "Paste!", Toast.LENGTH_SHORT).show(); } }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。