选择使用Android IME



在我的输入法服务中,我试图在当前光标位置之前选择文本。下面是代码片段

InputConnection inputConnection = getCurrentInputConnection();
ExtractedText extractedText = inputConnection.getExtractedText(new ExtractedTextRequest(), 0);
inputConnection.setSelection(extractedText.selectionStart-1,extractedText.selectionEnd);
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.updateSelection(null, extractedText.selectionStart-1,extractedText.selectionEnd, 0, 0);

这有一个非常古怪的行为,有时它选择,有时它只是把光标移回一步。

有谁能指出我做错了什么吗?

添加:

既然这个问题已经有一段时间没有答案了,我想提出另一个问题。我四处寻找一些选择文本的替代方法,在黑客键盘上,按shift键,然后按箭头键就可以了,但我无法复制这个过程。我尝试发送d键向下和向上键事件以及meta_shift_on标志。

但这不起作用…再说一遍,我做错了什么?

IME中选择的最佳解决方案

private void SelectionLeft() {
        ExtractedText extractedText = mLatinIme.getCurrentInputConnection().getExtractedText(new ExtractedTextRequest(), 0);
        if (extractedText == null || extractedText.text == null) return;
        int selectionStart = extractedText.selectionStart;
        int selectionEnd = extractedText.selectionEnd;

        mLatinIme.getCurrentInputConnection().setSelection(selectionStart, selectionEnd - 1);
    }
    private void SelectionRight() {
        ExtractedText extractedText = mLatinIme.getCurrentInputConnection().getExtractedText(new ExtractedTextRequest(), 0);
        if (extractedText == null || extractedText.text == null) return;
        int selectionStart = extractedText.selectionStart;
        int selectionEnd = extractedText.selectionEnd;
        mLatinIme.getCurrentInputConnection().setSelection(selectionStart, selectionEnd + 1);
    }

我用shift+箭头键解决了这个问题。

1 --> I was requesting the "inputConnection" for each event. I have now started using just one instance of inputConnection which I request for on the hook "onBindInput" and use it for all.
2 --> Sending Meta_shift_on as a flag along with the dpad_left/right/whatever was not enough, Now I send a press down shift event, then dpad up-down, and then up shift event. following is the pseudo-code: 

private void moveSelection(int dpad_keyCode) {   
inputMethodService.sendDownKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT, 0);
inputMethodService.sendDownAndUpKeyEvent(dpad_keyCode, 0);
inputMethodService.sendUpKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT, 0);
}

这就是它所需要的。

注意:这个解决方案或多或少是一个hack,我仍在寻找一个更好的解决方案,也允许我切换锚的选择。如果你知道更多,请分享。

inputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SHIFT_LEFT));
inputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_));
inputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SHIFKEYCODE_DPAD_T_LEFT));
inputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SHIFT_LEFT));

相关内容

  • 没有找到相关文章

最新更新