如何检测应用程序editext中的粘贴事件?

问题描述 投票:0回答:2

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

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

或者有其他解决方案可以解决这个问题吗?

android events paste
2个回答
14
投票

您可以设置监听器类别:

public interface GoEditTextListener {
void onUpdate();
}

С为 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
                }
            });

0
投票

正如Shivam 注意到的那样,接受的答案并不涵盖所有情况。 如今,有一个更好的解决方案,可以完美地处理粘贴事件。你需要实现像这样的

OnReceiveContentListener
接口:

@RequiresApi(api = Build.VERSION_CODES.S)
class CustomReceiver : OnReceiveContentListener {
    override fun onReceiveContent(view: View, payload: ContentInfo): ContentInfo {
        val pasteText = try {
            payload.clip.getItemAt(0).text // payload, we've got from clipboard
        } catch (e: IndexOutOfBoundsException) {
            ""
        }
        // put your logic here
        return payload
    }

    companion object {
        val MIME_TYPES = arrayOf("text/*")
    }
}

然后设置为EditText:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    editText.setOnReceiveContentListener(CustomReceiver.MIME_TYPES, CustomReceiver())
}

请记住,这仅适用于 API 级别 31 及以上。

© www.soinside.com 2019 - 2024. All rights reserved.