2017-04-08 114 views
1

我开发的软键盘为Android。 我想使用InputConnection.commitCorrecrion()更正一些文本,如果按键对应于Keyboard.KEYCODE_DONE被按下。 但是,文本不会改变,它只会闪烁一次。 我该如何解决这个问题?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代替(I被替换光标在任何字,如果有的话):

// 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) 
     } 
    ... 
相关问题