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

问题描述

我怎么能拦截此类事件?

How can I intercept this kind of events ?

我需要添加,当用户尝试一些文本粘贴到我的的EditText 我知道我可以使用 TextWatcher 但是这个入口点不是为我好监守我只需要拦截的情况下粘贴,而不是每次用户preSS我的的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:的

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

源读取救援!

我挖成的的TextView 的EditText 的TextView 有一些不同的配置),并且发现了用于提供剪切/复制/粘贴选项的菜单只是一个关于修改<$c$c>ContextMenu (<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3_r1/android/widget/TextView.java#6756">source).

作为一个正常的上下文菜单,查看必须创建菜单(<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3_r1/android/widget/TextView.java#7386">source)然后处理的回调方法的相互作用(<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3_r1/android/widget/TextView.java#7516">source).

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

由于处理方法是公开,我们可以简单地钩到它通过扩展的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();
    }
}

现在,当用户使用剪切/复制/粘贴,一个吐司显示(当然你可以做其他的事情,太)。

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).

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

08-26 20:08
查看更多