Я использую BaseInputConnection для реализации предложений по вводу для пользовательского виджета ввода текста. Однако возврат/деление не обрабатываются автоматически, поэтому я реализую их в sendKeyEvent() следующим образом:
public class CustomInputConnection extends BaseInputConnection {
public CustomInputConnection(View view, boolean fullEditor) {
super(view, fullEditor);
}
@Override
public boolean sendKeyEvent(KeyEvent event) {
int action = event.getAction();
int keycode = event.getKeyCode();
// If this is backspace/del or if the key has a character representation,
// need to update the underlying Editable (i.e. the local representation of the text
// being edited). Some IMEs like Jellybean stock IME and Samsung IME mix in delete
// KeyPress events instead of calling deleteSurroundingText.
if (action == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_DEL) {
Log.i("Foo", "delete < ");
deleteSurroundingText(1, 0);
} else if (action == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_FORWARD_DEL) {
Log.i("Foo", "delete >");
deleteSurroundingText(0, 1);
} else if (action == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_ENTER) {
Log.i("Foo", "enter");
// None of these works:
//setComposingText("", 0);
//beginBatchEdit();
//finishComposingText();
//endBatchEdit();
//CharSequence beforCursorText = getTextBeforeCursor(100, 0);
//CharSequence afterCursorText = getTextAfterCursor(100, 0);
//deleteSurroundingText(beforCursorText.length(), afterCursorText.length());
//deleteSurroundingText(100, 100);
}
return true;
}
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
Log.i("Foo", "onCommitText: " + text.toString());
return true;
}
@Override
public boolean endBatchEdit() {
String text = getTextBeforeCursor(100, 0).toString();
Log.i("Foo", "onEditText: " + text);
return super.endBatchEdit();
}
}
Когда я набираю «привет», затем нажимаю и набираю «abc», я ожидаю увидеть «abc», но вместо этого пишет «helloabc». Мне не удалось получить эту работу.
Вот пример вывода:
onEditText: h
onEditText: h
onEditText: he
onEditText: he
onEditText: hel
onEditText: hel
onEditText: hel
onEditText: hell
onEditText: hell
onEditText: hell
onEditText: hello
onEditText: hello
enter
onEditText:
onEditText:
onEditText: helloa
onEditText: helloa
onEditText: helloab
onEditText: helloab
onEditText: helloabx
onEditText: helloabx
Вот как я создаю InputConnection:
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT
| EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT;
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN
| EditorInfo.IME_ACTION_DONE;
// fullEditor is false, but we might set this to true for enabling
// text selection, and copy/paste. Lets see.
return new CustomInputConnection(this, false);
}
Что я делаю не так? Как мне заставить это работать?
Как будто IME запоминает введенные данные и игнорирует входное соединение.
Я хочу чтобы очистить предложения по вводу и полностью сбросить состояние BaseInputConnection. Как мне это сделать?
Подробнее здесь: https://stackoverflow.com/questions/792 ... hen-pressi
Android - исправление четких предложений кандидатов в BaseInputConnection при нажатии Enter ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Android - исправление четких предложений кандидатов в BaseInputConnection при нажатии Enter
Anonymous » » в форуме JAVA - 0 Ответы
- 9 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Невозможно отправить значение MudTextfield после HIT Enter Enter Key в Mudblazor
Anonymous » » в форуме C# - 0 Ответы
- 10 Просмотры
-
Последнее сообщение Anonymous
-