How To Commit Composing Text To An Inputconnection When The User Changes The Selection
I am making a custom keyboard and have to set composing text before committing it. This is described in this Q&A. I know how to commit text in general inputConnection.commitTex
Solution 1:
The editor (EditText
, etc.) calls updateSelection
on the InputMethodManager
, which in turn notifies the onUpdateSelection
listener. Thus, the keyboard can override onUpdateSelection
and take care of the unfinished composing span there.
To handle an unfinished composing span you can use finishComposingText
on the InputConnection
. This will remove the composing span and commit whatever text was in the span.
Here is how it is implemented in the sample Android soft keyboard:
/**
* Deal with the editor reporting movement of its cursor.
*/@OverridepublicvoidonUpdateSelection(int oldSelStart, int oldSelEnd,
int newSelStart, int newSelEnd,
int candidatesStart, int candidatesEnd) {
super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd,
candidatesStart, candidatesEnd);
// If the current selection in the text view changes, we should// clear whatever candidate text we have.if (mComposing.length() > 0 && (newSelStart != candidatesEnd
|| newSelEnd != candidatesEnd)) {
mComposing.setLength(0);
updateCandidates();
InputConnectionic= getCurrentInputConnection();
if (ic != null) {
ic.finishComposingText();
}
}
}
Solution 2:
intcandStart= -1;
intcandEnd= -1;
if (mTextView.getText() instanceof Spannable) {
finalSpannablesp= (Spannable) mTextView.getText();
candStart = EditableInputConnection.getComposingSpanStart(sp);
candEnd = EditableInputConnection.getComposingSpanEnd(sp);
}
Post a Comment for "How To Commit Composing Text To An Inputconnection When The User Changes The Selection"