Android在editText上拦截粘贴复制剪切 [英] Android intercept pastecopycut on editText

查看:92
本文介绍了Android在editText上拦截粘贴复制剪切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何拦截此类事件?

当用户尝试将一些文本粘贴到我的 EditText 中时,我需要添加一些逻辑我知道我可以使用 TextWatcher 但这个入口点对我不利,因为我只在粘贴的情况下需要拦截,而不是每次用户按下我的 EditText,

I need to add some logic when the user trying to paste some text into my EditText i know i can use TextWatcher but this entrypoint not good for me becuase i only need to intercept in case of paste and not every time the user press my EditText,

推荐答案

使用 API 似乎没什么可做的:android粘贴事件

Seems there isn't much you can do by using the API: android paste event

来源阅读救援!

我挖掘了 TextView 的 Android 源码(EditText 是一个 TextView 有一些不同的配置),发现菜单使用提供剪切/复制/粘贴选项只是修改后的 ContextMenu (来源).

I dug into the Android Source of the TextView (EditText is a TextView with some different configuration) and found out that the menu used to offer the cut/copy/paste options is just a modified ContextMenu (source).

对于普通的上下文菜单,视图必须创建菜单(source) 然后在回调方法中处理交互 (来源).

As for a normal context-menu, the View must create the menu (source) and then handle the interaction in a callback-method (source).

因为处理方法是 public,我们可以简单地通过扩展 EditText 并覆盖该方法以对不同的操作做出反应来挂钩它.这是一个示例实现:

Because the handling method is public, we can simply hook into it by extending EditText and overwriting the method to react on the different actions. Here is an example-implementation:

import android.content.Context;
import android.util.AttributeSet;
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
 */
public class MonitoringEditText extends EditText {

    private final Context context;

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

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

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

    /**
     * <p>This is where the "magic" happens.</p>
     * <p>The menu used to cut/copy/paste is a normal ContextMenu, which allows us to
     *  overwrite the consuming method and react on the different events.</p>
     * @see <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3_r1/android/widget/TextView.java#TextView.onTextContextMenuItem%28int%29">Original Implementation</a>
     */
    @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(context, "Cut!", Toast.LENGTH_SHORT).show();
    }

    /**
     * Text was copied from this EditText.
     */
    public void onTextCopy(){
        Toast.makeText(context, "Copy!", Toast.LENGTH_SHORT).show();
    }

    /**
     * Text was pasted into the EditText.
     */
    public void onTextPaste(){
        Toast.makeText(context, "Paste!", Toast.LENGTH_SHORT).show();
    }
}

现在,当用户使用剪切/复制/粘贴时,会显示一个 Toast(当然你也可以做其他事情).

Now, when the user uses cut/copy/paste, a Toast is shown (of course you could do other things, too).

巧妙的是,这适用于 Android 1.5,您无需重新创建上下文菜单(如上述链接问题中所建议的那样),这将 保持平台外观不变(例如使用 HTC Sense).

The neat thing is that this works down to Android 1.5 and you don't need to re-create the context-menu (like suggested in the above linked question), which will keep the constant look of the platform (for example with HTC Sense).

这篇关于Android在editText上拦截粘贴复制剪切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆