2017-04-08 59 views
1

Android用ソフトキーボードを開発しています。 Keyboard.KEYCODE_DONEに対応するキーが押された場合、InputConnection.commitCorrecrion()を使用してテキストを修正したいと思います。 しかし、テキストは変更されず、一度点滅します。 この問題を解決するにはどうすればよいですか?InputConnection.commitCorrection()が正常に動作しないようです。

public class SimpleIME extends InputMethodService 
    implements KeyboardView.OnKeyboardActionListener { 
.... 

@Override 
public void onKey(int primaryCode, int[] keyCodes) { 
    InputConnection ic = getCurrentInputConnection(); 
    switch(primaryCode){ 
    .... 

     case Keyboard.KEYCODE_DONE: 
      ic.commitCorrection(new CorrectionInfo(oldTextPosition, oldText, newText)); 
      break; 
    .... 
    } 
} 

答えて

0

私は同様の問題が、フラッシュなし、ですべてのテキストにだけ変更なしでした。私はのEditTextの入力は、単にこのcommitCorrection呼び出しに応答していないか、何らかの理由でそれを受け入れていなかったことを決めたので、私は(もしあれば、私は、カーソルがでたどんな単語置き換えた)代わりにdeleteSurroundingTextとcommitTextを使用:

// limit=0 allows trailing empty strings to represent end-of-string split 
    fun getWordBeforeCursor() : String { 
     val arr = wordbreak.split(ic.getTextBeforeCursor(255, 0), 0) 
     Log.d(TAG, "words before: " + arr.joinToString(",")) 
     return if (arr.isEmpty()) "" else arr.last() 
    } 

    fun getWordAfterCursor() : String { 
     val arr = wordbreak.split(ic.getTextAfterCursor(255, 0), 0) 
     Log.d(TAG, "words after: " + arr.joinToString(",")) 
     return if (arr.isEmpty()) "" else arr.first() 
    } 


    fun getCursorPosition() : Int { 
     val extracted: ExtractedText = ic.getExtractedText(ExtractedTextRequest(), 0); 
     return extracted.startOffset + extracted.selectionStart; 
    } 

    try { 
     ... 

     val backward = getWordBeforeCursor() 
     val forward = getWordAfterCursor() 

     Log.d(TAG, "cursor position: " + getCursorPosition()) 
     Log.d(TAG, "found adjacent text: [" + backward + "_" + forward + "]") 

     // if in the middle of a word, delete it 
     if (forward.isNotEmpty() && backward.isNotEmpty()) { 
      //instead of this: 
      //ic.commitCorrection(CorrectionInfo(getCursorPosition()-backward.length, backward + forward, icon.text)) 

      //do this: 
      ic.deleteSurroundingText(backward.length, forward.length) 
      ic.commitText(newText, 1) 
     } 
    ... 
関連する問題