Android IME,获取EditText实例

发布于 2024-11-26 18:01:06 字数 747 浏览 0 评论 0原文

我目前正在开发一个输入法,它需要知道文本内容才能计算出光标所在的行和字符等信息。这适用于全屏模式下的提取文本,但我不想强制全屏。这是我当前实现的相关代码

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'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 current 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));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-12-03 18:01:06

我查看了 InputMethodService 的源代码,发现它如何提取文本以供 ExtractEditText 使用,因此我能够创建一个解决方案:

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();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文