Android - catch backspace (delete) button on custo

2019-07-18 02:01发布

I want to catch the backspace/delete button event in soft keyboard on EditText custom.

How can I do it ?

I tried these solutions but they do not work for me :

Android custom EditText and back button override

EditText OnKeyDown

Get back key event on EditText

Android EditText delete(backspace) key event

Thanks for your help !


EDIT : I found a fix for that isssue with the function DispatchKeyEvent :

@Override public boolean dispatchKeyEvent(@NonNull KeyEvent event) { 
    if (event.getAction() == KeyEvent.ACTION_DOWN
        && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
     // ... 
    }
    return super.dispatchKeyEvent(event);
}

4条回答
Rolldiameter
2楼-- · 2019-07-18 02:42

I found a fix for that isssue with the function DispatchKeyEvent :

@Override public boolean dispatchKeyEvent(@NonNull KeyEvent event) { 
    if (event.getAction() == KeyEvent.ACTION_DOWN
        && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
        // ... 
    }
    return super.dispatchKeyEvent(event);
}
查看更多
女痞
3楼-- · 2019-07-18 02:53

Please, take a look at a solution I provided in another similar topic: Android - cannot capture backspace/delete press in soft. keyboard. You need to build your custom EditText class and override onCreateInputConnection method in it. It will give you access to deleteSurroundingText event which is firing when you click backspace. I've tested it in some devices and hope it will work in others also. Try it and give a feedback.

查看更多
forever°为你锁心
4楼-- · 2019-07-18 02:55

Here you go..

editText.addTextChangedListener(new TextWatcher() {

            private int before;
            private int after;
            private int prevLength;
            private boolean isBackPressed;
            ArrayList<Integer> arrayList;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                prevLength = charSequence.length();
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int end, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                isBackPressed = prevLength > editable.length();

                if(isBackPressed){
                    Toast.makeText(getApplicationContext(),"Back",Toast.LENGTH_LONG).show();
                }
            }
        });
查看更多
劫难
5楼-- · 2019-07-18 02:57

Catching Key Events from soft input methods in Android is unreliable. Here is an excerpt from the JavaDoc for the KeyEvent class:

As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier.

One workaround could involve using TextWatcher. Whenever a delete occurs in an EditText, the character count drops.

myEditText.addTextChangedListener(this);
//...
public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (count == 0) {
        //text was deleted. 
    }
}

Edit: This is untested, so may not be completely reliable. In addition, it's worth pointing out that count may also read 0 if a long section of text is selected and then replaced. I created a library once that was designed to add Undo/Redo functionality to EditText, but can also identify the difference between text being replaced and deleted, so this may be of use to you if you require more accuracy.

查看更多
登录 后发表回答