开发者

Android IME, get EditText instance

I'm currently developing an IME, and it needs to know the text content to work out things like what line and character the cursor is on. This works with the Extract text in fullscreen mode but I would like to not have to enforce fullscreen. Here's related code of my 开发者_如何学Ccurrent implementation:

private ExtractEditText mExtract;

...

    mExtract = new ExtractEditText(this);
    mExtract.setId(android.R.id.inputExtractEditText);
    setExtractView(mExtract);

...

@Override public void onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) {
    super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, candidatesEnd);
    String textToMeasure = mExtract.getText().toString().substring(0, newSelStart);
    Log.w("myIME", "Line: " + countLines(textToMeasure));
}


I looked in the source of InputMethodService and found out how it extracts the text for the use of the ExtractEditText, thus I was able to create a solution:

private String getExtractText() {
    ExtractedTextRequest req = new ExtractedTextRequest();
    req.token = 0;
    req.flags = InputConnection.GET_TEXT_WITH_STYLES;
    req.hintMaxLines = 10;
    req.hintMaxChars = 10000;
    ExtractedText et = getCurrentInputConnection().getExtractedText(req, InputConnection.GET_EXTRACTED_TEXT_MONITOR);
    return et.text.toString();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜