I'm following this example: https://android.googlesource.com/platform/development/+/master/samples/SoftKeyboard/
I'm having trouble while adding a clear text button. I want a button which clear in-focus text. But since I re-focus a not-empty text, I don't know how to delete existed characters. For example I have editText A and editText B.
focus A > commit "hello" > focus B > commit "world" > focus A > clear text in A >> FAIL
I still can delete text in A one by one character by using:
//keyEventCode = KeyEvent.KEYCODE_DEL
getCurrentInputConnection().sendKeyEvent(
new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
getCurrentInputConnection().sendKeyEvent(
new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
But it's impossible to clear text A since text A length is unknown. In addition, KeyEvent.KEYCODE_CLEAR doesn't work with above function.
Any suggestion may help, thank you very much.
I did something like this:
InputConnection inputConnection = getCurrentInputConnection();
CharSequence currentText = inputConnection.getExtractedText(new ExtractedTextRequest(), 0).text;
CharSequence beforCursorText = inputConnection.getTextBeforeCursor(currentText.length(), 0);
CharSequence afterCursorText = inputConnection.getTextAfterCursor(currentText.length(), 0);
inputConnection.deleteSurroundingText(beforCursorText.length(), afterCursorText.length());
How about you simply retrieve the view under focus using getCurrentFocus()
(see documentation) and then call myFocusedEditText.setText("")
(I assume your field is an EditText)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With