2017-04-08 13 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) 
     } 
    ...