Android自定义软键盘 - 如何清除编辑文本(提交文本,...)

Anh*_*Mai 4 android textview android-softkeyboard android-edittext

我正在关注这个例子:https://android.googlesource.com/platform/development/+/master/samples/SoftKeyboard/

我在添加明文按钮时遇到了麻烦.我想要一个清除焦点文本的按钮.但是由于我重新关注一个非空文本,我不知道如何删除现有的字符.例如,我有editText A和editText B.

focus A > commit "hello" > focus B > commit "world" > focus A > clear text in A >> FAIL
Run Code Online (Sandbox Code Playgroud)

我仍然可以使用以下方法删除A中逐个字符的文本:

    //keyEventCode = KeyEvent.KEYCODE_DEL
    getCurrentInputConnection().sendKeyEvent(
            new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
    getCurrentInputConnection().sendKeyEvent(
            new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
Run Code Online (Sandbox Code Playgroud)

但是,由于文本长度未知,因此无法清除文本A. 此外,KeyEvent.KEYCODE_CLEAR不适用于上述功能.

任何建议都可能有所帮助,非常感谢.

小智 6

我做了这样的事情:

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());
Run Code Online (Sandbox Code Playgroud)