2013-02-20 83 views
27

如何拦截这类事件?Android拦截在editText上粘贴 copy cut

我需要添加当用户试图粘贴一些文本到我EditText我知道我可以使用TextWatcher但这个入口点不是为我好监守我只需要粘贴的情况下拦截用户每次一些逻辑,而不是按我的EditText

+0

好挑战。看看我的答案。 – 2013-02-20 13:45:49

回答

62

似乎没有太多的你可以使用API​​做的事:android paste event

Source reading to the rescue!

我挖成TextViewEditText的Android的源代码是TextView),并发现用于提供剪切/复制/粘贴选项的菜单只是一个修改的ContextMenusource)。

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

因为处理方法是public,我们可以通过扩展EditText来简单地挂钩它,并覆盖方法以对不同的动作作出反应。下面是一个例子的实现:

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显示(当然你可以做其他的事情,太)。

的妙处是,这个工程下来到Android 1.5,你不需要重新创建上下文菜单(如建议在上面的链接的问题),这将保持在恒定的样子平台(例如HTC Sense)。

+0

我得到**不能实例化类没有空的构造函数**错误是什么解决方案? – 2014-07-11 11:50:57

+0

好吧,类没有空的构造函数。你必须传递一个'Context'的实例(如果你的代码在一个Activity中,你可以传递'this')。 – 2014-07-11 16:32:43

+0

用侦听器扩展您的解决方案,以便它可以用作即插即用组件。要点https://gist.github.com/guillermomuntaner/82491cbf0c88dec560a5 – GuillermoMP 2016-01-17 21:12:51

0

虽然不是100%可靠,但有一种更简单的方法。

添加TextChangedListener到您的编辑框:

EditText et = (EditText) mView.findViewById(R.id.yourEditText); 
et.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    if (count > 2) toast("text was pasted"); 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

    public void afterTextChanged(Editable s) { 

    } 
}); 

如果超过2个字符文本的变化,你可以假定它粘贴(一些表情符号占用两个字符)。

当用户粘贴1或2个字符时,它当然不会检测粘贴,并且如果文本中的更改由其他内容触发,它将错误地报告粘贴。

但是对于大多数目的而言,它可以完成工作。