How do I define an insert event in an editext application?

How to detect when the user is copying data and pasting it into the edittext of the application. You only need to detect the insert event.

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

Or is there any other solution to solve this problem?

+3


source to share


2 answers


You can set a listener class:

public interface GoEditTextListener {
void onUpdate();
}

      

Create your own 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"/>

      

And in your activity:

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

      

+5


source


It is not possible to add a comment (deplorable lack of reputation) by adding an answer. refer to this link:

Patch to intercept Android \ copy \ cut on editText



there are several questions (and accepted answers) about this topic. Please refer to them before posting a question, and if you mentioned, mention it!

+4


source







All Articles