下面列出了android.view.inputmethod.InputConnection#setComposingText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Keyboard.OnKeyboardListener method
*
* @param choice of the item that was chosen from the candidate list
* when the keyboard key was long pressed.
*/
@Override
public void onKeyPopupChosen(PopupKeyCandidate choice) {
InputConnection ic = getInputConnection();
if (ic == null) return;
if (choice == null) return;
String composingText = choice.getComposing();
String unicode = choice.getUnicode();
if (TextUtils.isEmpty(composingText)) {
onKeyboardInput(unicode);
} else {
checkForFinishedWord(unicode);
boolean isMongol = MongolCode.isMongolian(unicode.charAt(0));
handleOldComposingText(isMongol);
ic.setComposingText(composingText, 1);
mComposing = unicode;
}
}
public void revertLastWord(boolean deleteChar) {
final int length = mComposing.length();
if (!mPredicting && length > 0) {
final InputConnection ic = getCurrentInputConnection();
mPredicting = true;
mJustRevertedSeparator = ic.getTextBeforeCursor(1, 0);
if (deleteChar)
ic.deleteSurroundingText(1, 0);
int toDelete = mCommittedLength;
CharSequence toTheLeft = ic
.getTextBeforeCursor(mCommittedLength, 0);
if (toTheLeft != null && toTheLeft.length() > 0
&& isWordSeparator(toTheLeft.charAt(0))) {
toDelete--;
}
ic.deleteSurroundingText(toDelete, 0);
ic.setComposingText(mComposing, 1);
TextEntryState.backspace();
postUpdateSuggestions();
} else {
sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
mJustRevertedSeparator = null;
}
}
/**
* Append newText to the text field represented by connection.
* The new text becomes selected.
*/
public static void appendText(InputConnection connection, String newText) {
if (connection == null) {
return;
}
// Commit the composing text
connection.finishComposingText();
// Add a space if the field already has text.
CharSequence charBeforeCursor = connection.getTextBeforeCursor(1, 0);
if (charBeforeCursor != null
&& !charBeforeCursor.equals(" ")
&& (charBeforeCursor.length() > 0)) {
newText = " " + newText;
}
connection.setComposingText(newText, 1);
}
public static ViewAction forceComposingText(final String text) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(ContactsCompletionView.class);
}
@Override
public String getDescription() {
return null;
}
@Override
public void perform(UiController uiController, View view) {
ContactsCompletionView completionView = (ContactsCompletionView)view;
InputConnection connection = completionView.testAccessibleInputConnection;
connection.setComposingText(text, -1);
}
};
}
/**
* Try to replace the current word with its substitution.
*/
private void replaceText(InputConnection con) {
ExtractedText txt = con.getExtractedText(new ExtractedTextRequest(), 0);
if (txt != null) {
int end = txt.text.toString().indexOf(" ", txt.selectionEnd);
if (end == -1) {
end = txt.text.length();
}
int start = txt.text.toString().lastIndexOf(" ", txt.selectionEnd - 1);
start++;
String sel = txt.text.subSequence(start, end).toString();
String rep = myService.replacements.get(sel);
if (rep != null) {
con.setComposingRegion(start, end);
con.setComposingText(rep, 1);
con.finishComposingText();
}
else {
String err = myService.getResources().getString(
R.string.err_no_replacement, sel);
Toast.makeText(myService, err, Toast.LENGTH_SHORT).show();
}
}
}
/**
* Updates the composing text based on the braille dots composed thus far,
* and maintains the composing state of the editor.
* Returns {@code true} if the current string of braille dots could be
* translated into text, otherwise {@code false}.
*/
private boolean updateComposingText(@NonNull BrailleTranslator translator,
@NonNull InputConnection ic) {
if (mComposingBraille.position() == 0) {
return ic.commitText("", 1);
}
String text = translator.backTranslate(getComposingBrailleArray());
if (TextUtils.isEmpty(text)) {
return ic.setComposingText("\u00A0", 1);
} else {
return ic.setComposingText(text, 1);
}
}
/**
* use ROT13 to scramble the contents of the editor
*/
private void scramble(InputConnection con) {
char[] buffer = null;
CharSequence selected = con.getSelectedText(0);
if (selected != null) {
buffer = selected.toString().toCharArray();
}
else {
ExtractedText txt = con.getExtractedText(new ExtractedTextRequest(), 0);
if (txt == null) {
return;
}
buffer = txt.text.toString().toCharArray();
if (buffer.length == 0)
return;
}
// char[] buffer = con.getSelectedText(0).toString().toCharArray();
// //con.getExtractedText(new
// ExtractedTextRequest(),0).text.toString().toCharArray();
for (int i = 0; i < buffer.length; i++) {
if (buffer[i] >= 'a' && buffer[i] <= 'm')
buffer[i] += 13;
else if (buffer[i] >= 'A' && buffer[i] <= 'M')
buffer[i] += 13;
else if (buffer[i] >= 'n' && buffer[i] <= 'z')
buffer[i] -= 13;
else if (buffer[i] >= 'N' && buffer[i] <= 'Z')
buffer[i] -= 13;
}
if (selected == null) {
con.setComposingRegion(0, buffer.length);
}
con.setComposingText(new String(buffer), 1);
con.finishComposingText();
}
private void handleBackspace() {
boolean deleteChar = false;
InputConnection ic = getCurrentInputConnection();
if (ic == null)
return;
ic.beginBatchEdit();
if (mPredicting) {
final int length = mComposing.length();
if (length > 0) {
mComposing.delete(length - 1, length);
mWord.deleteLast();
ic.setComposingText(mComposing, 1);
if (mComposing.length() == 0) {
mPredicting = false;
}
postUpdateSuggestions();
} else {
ic.deleteSurroundingText(1, 0);
}
} else {
deleteChar = true;
}
postUpdateShiftKeyState();
TextEntryState.backspace();
if (TextEntryState.getState() == TextEntryState.State.UNDO_COMMIT) {
revertLastWord(deleteChar);
ic.endBatchEdit();
return;
} else if (mEnteredText != null
&& sameAsTextBeforeCursor(ic, mEnteredText)) {
ic.deleteSurroundingText(mEnteredText.length(), 0);
} else if (deleteChar) {
if (mCandidateView != null
&& mCandidateView.dismissAddToDictionaryHint()) {
// Go back to the suggestion mode if the user canceled the
// "Touch again to save".
// NOTE: In gerenal, we don't revert the word when backspacing
// from a manual suggestion pick. We deliberately chose a
// different behavior only in the case of picking the first
// suggestion (typed word). It's intentional to have made this
// inconsistent with backspacing after selecting other
// suggestions.
revertLastWord(deleteChar);
} else {
sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
if (mDeleteCount > DELETE_ACCELERATE_AT) {
sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
}
}
}
mJustRevertedSeparator = null;
ic.endBatchEdit();
}
private void handleCharacter(int primaryCode, int[] keyCodes) {
if (mLastSelectionStart == mLastSelectionEnd
&& TextEntryState.isCorrecting()) {
abortCorrection(false);
}
if (isAlphabet(primaryCode) && isPredictionOn()
&& !mModCtrl && !mModAlt && !mModMeta
&& !isCursorTouchingWord()) {
if (!mPredicting) {
mPredicting = true;
mComposing.setLength(0);
saveWordInHistory(mBestWord);
mWord.reset();
}
}
if (mModCtrl || mModAlt || mModMeta) {
commitTyped(getCurrentInputConnection(), true); // sets mPredicting=false
}
if (mPredicting) {
if (isShiftCapsMode()
&& mKeyboardSwitcher.isAlphabetMode()
&& mComposing.length() == 0) {
// Show suggestions with initial caps if starting out shifted,
// could be either auto-caps or manual shift.
mWord.setFirstCharCapitalized(true);
}
mComposing.append((char) primaryCode);
mWord.add(primaryCode, keyCodes);
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
// If it's the first letter, make note of auto-caps state
if (mWord.size() == 1) {
mWord.setAutoCapitalized(getCursorCapsMode(ic,
getCurrentInputEditorInfo()) != 0);
}
ic.setComposingText(mComposing, 1);
}
postUpdateSuggestions();
} else {
sendModifiableKeyChar((char) primaryCode);
}
updateShiftKeyState(getCurrentInputEditorInfo());
TextEntryState.typedCharacter((char) primaryCode,
isWordSeparator(primaryCode));
}