【问题标题】:Selection using Android IME使用 Android IME 进行选择
【发布时间】:2017-03-24 02:39:20
【问题描述】:

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

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-pad 键向下和向上键事件与 meta_shift_on 标志一起发送。

但这不起作用...再次,我做错了什么?

【问题讨论】:

    标签: android android-softkeyboard android-input-method


    【解决方案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,我仍在寻找更好的解决方案,它还允许我切换选择的锚点。如果你知道的更好,请分享。

    【讨论】:

    • inputMethodService的问题如何初始化?
    【解决方案2】:
    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));
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 如何上下选择??
      • 这看起来很有趣,我要试一试。谢谢。
      猜你喜欢
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      相关资源
      最近更新 更多