如何在应用程序的editext中检测粘贴事件? [英] How to detect the paste event in editext of the application?

查看:97
本文介绍了如何在应用程序的editext中检测粘贴事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测用户何时复制数据并将其粘贴到应用程序的edittext中。只需要检测粘贴事件。

How to detect when a user copies a data and paste it in the edittext of the application. Only need to detect the paste event.

例如:当用户从手机中保存的音符中复制信用卡详细信息并将其粘贴到相应的应用程序编辑文本中时,只能检测到它粘贴事件?

For example: When a user copies the credit card details from a saved note in the phone and paste it in the corresponding edittext of application, how can we detect it, only the paste event?

还有其他解决方案可以解决吗?

Or is there any other solution is available for to solve this?

推荐答案

您可以设置Listener类:

You can set Listener Class:

public interface GoEditTextListener {
void onUpdate();
}

为EditText创建自学课程:

Сreate self class for EditText:

public class GoEditText extends EditText
{
    ArrayList<GoEditTextListener> listeners;

    public GoEditText(Context context)
    {
        super(context);
        listeners = new ArrayList<>();
    }

    public GoEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        listeners = new ArrayList<>();
    }

    public GoEditText(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        listeners = new ArrayList<>();
    }

    public void addListener(GoEditTextListener listener) {
        try {
            listeners.add(listener);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    /**
     * Here you can catch paste, copy and cut events
     */
    @Override
    public boolean onTextContextMenuItem(int id) {
        boolean consumed = super.onTextContextMenuItem(id);
        switch (id){
            case android.R.id.cut:
                onTextCut();
                break;
            case android.R.id.paste:
                onTextPaste();
                break;
            case android.R.id.copy:
                onTextCopy();
        }
        return consumed;
    }

    public void onTextCut(){
    }

    public void onTextCopy(){
    }

    /**
     * adding listener for Paste for example
     */
    public void onTextPaste(){
        for (GoEditTextListener listener : listeners) {
            listener.onUpdate();
        }
    }
}

xml:

<com.yourname.project.GoEditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/editText1"/>

在您的活动中:

private GoEditText editText1;

editText1 = (GoEditText) findViewById(R.id.editText1);

            editText1.addListener(new GoEditTextListener() {
                @Override
                public void onUpdate() {
//here do what you want when text Pasted
                }
            });

这篇关于如何在应用程序的editext中检测粘贴事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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