How to make delete key- delete all selected edit text in custom android keyboard

痞子三分冷 提交于 2019-12-10 23:06:03

问题


I am creating custom android keyboard and I want delete key to delete all the edit text if they are selected.

i.e, when text is selected, clipboard(cut,copy,paste comes up); in that mode , if the delete key is pressed, it should delete all. It doesn't currently.

P.S don't tell me for a specific edit text, it is a custom keyboard, it won't have access to edit text.


回答1:


I don't see why it won't. In the keyboard, I created, I just send out the delete key event and it works like a charm. Try the following when delete/backspace key is pressed while something is selected.

getCurrentInputConnection().sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DEL));
getCurrentInputConnection().sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_DEL));



回答2:


You can also see if the InputConnection has any selected text with getSelectedText. Then delete it if it does (or delete the preceding character if it doesn't).

CharSequence selectedText = inputConnection.getSelectedText(0);
if (TextUtils.isEmpty(selectedText)) {
    inputConnection.deleteSurroundingText(1, 0);
} else {
    inputConnection.commitText("", 1);
}


来源:https://stackoverflow.com/questions/38005041/how-to-make-delete-key-delete-all-selected-edit-text-in-custom-android-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!