Android 自定义软键盘 - 如何清除编辑文本(提交的文本,...)
Android custom soft keyboard - how to clear edit text (commited text,...)
我正在关注这个例子: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
我仍然可以使用以下方法逐个字符删除 A 中的文本:
//keyEventCode = KeyEvent.KEYCODE_DEL
getCurrentInputConnection().sendKeyEvent(
new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
getCurrentInputConnection().sendKeyEvent(
new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
但无法清除文本 A,因为文本 A 的长度未知。此外,KeyEvent.KEYCODE_CLEAR 不适用于上述功能。
任何建议都可能有所帮助,非常感谢。
您如何简单地使用 getCurrentFocus()
(参见 documentation)检索焦点下的视图,然后调用 myFocusedEditText.setText("")
(我假设您的字段是 EditText)?
我做了这样的事情:
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());
我正在关注这个例子: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
我仍然可以使用以下方法逐个字符删除 A 中的文本:
//keyEventCode = KeyEvent.KEYCODE_DEL
getCurrentInputConnection().sendKeyEvent(
new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
getCurrentInputConnection().sendKeyEvent(
new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
但无法清除文本 A,因为文本 A 的长度未知。此外,KeyEvent.KEYCODE_CLEAR 不适用于上述功能。
任何建议都可能有所帮助,非常感谢。
您如何简单地使用 getCurrentFocus()
(参见 documentation)检索焦点下的视图,然后调用 myFocusedEditText.setText("")
(我假设您的字段是 EditText)?
我做了这样的事情:
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());