2016-11-10 12 views
2

入力メソッドのサービスで、現在のカーソル位置の前にテキストを選択しようとしています。以下はコードスニペットです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); 

これは非常に不安定な振る舞いをしています。時にはカーソルを一歩前に戻すこともあります。

誰かが私が間違っていることを指摘できますか?

ADDITION:この質問は、しばらくの間未解決の残っているので

、私は別の問題を提起したいと思います。私はテキストを選択するいくつかの代替方法を探していました。ハッカーのキーボードでシフトを押してから矢印キーを押すとトリックはしますが、プロセスを複製することはできません。私はmeta_shift_onフラグと一緒にキーパッドを押して、キーパッドを押してみました。

しかし、それは動作していません...再び、私は間違っていますか?

答えて

0

シフト+矢印キーを使用してこの問題を解決しました。

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

これだけですべてです。

注:このソリューションは多かれ少なかれハックですが、私は選択肢のアンカーを切り替えることも可能にする優れたソリューションを探しています。あなたが良く知っているなら、分かち合ってください。

関連する問題