Better way to get all the text in an EditText from an InputConnection?

泄露秘密 提交于 2019-12-03 09:26:44

问题


I've written an IME (InputMethodService) and I need to get all the text from the EditText it is editing. I know one way:

InputConnection inputConnection = getCurrentInputConnection();
inputConnection.append(inputConnection.getTextBeforeCursor(9999, 0))
.append(inputConnection.getTextAfterCursor(9999, 0));

It works, but it seems pretty stupid and clunky. However there is no such method InputConnection.getText().

Is there a better way?

P.S. I cannot access the EditText from my IME because it belongs to the parent app so please don't tell me to use EditText.getText(), unless you know a way to get the EditText!


回答1:


Here is also another way to do it:

inputConnection.performContextMenuAction(android.R.id.selectAll);
CharSequence sData =  inputConnection.getSelectedText(0);



回答2:


My solution is to use getExtractedText(). I am not sure if this has some limitations, but has worked for me so far.

CharSequence currentText = inputConnection.getExtractedText(new ExtractedTextRequest(), 0).text;


来源:https://stackoverflow.com/questions/7440269/better-way-to-get-all-the-text-in-an-edittext-from-an-inputconnection

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