下面列出了android.view.inputmethod.InputConnection#setSelection ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* This is called when the user has moved the cursor in the extracted
* text view, when running in fullsreen mode. The default implementation
* performs the corresponding selection change on the underlying text
* editor.
*/
public void onExtractedSelectionChanged(int start, int end) {
InputConnection conn = getCurrentInputConnection();
if (conn != null) {
conn.setSelection(start, end);
}
}
/**
* @hide
*/
public void onExtractedDeleteText(int start, int end) {
InputConnection conn = getCurrentInputConnection();
if (conn != null) {
conn.finishComposingText();
conn.setSelection(start, start);
conn.deleteSurroundingText(0, end - start);
}
}
/**
* @hide
*/
public void onExtractedSetSpan(Object span, int start, int end, int flags) {
InputConnection conn = getCurrentInputConnection();
if (conn != null) {
if (!conn.setSelection(start, end)) return;
CharSequence text = conn.getSelectedText(InputConnection.GET_TEXT_WITH_STYLES);
if (text instanceof Spannable) {
((Spannable) text).setSpan(span, 0, text.length(), flags);
conn.setComposingRegion(start, end);
conn.commitText(text, 1);
}
}
}
@Override
public void moveCursorEnd() {
InputConnection ic = getInputConnection();
if (ic == null) return;
ExtractedText extractedText = ic.getExtractedText(new ExtractedTextRequest(), 0);
if (extractedText == null || extractedText.text == null) return;
int length = extractedText.text.length();
ic.setSelection(length, length);
}
@Override
public void selectWordBack() {
InputConnection ic = getInputConnection();
if (ic == null) return;
ExtractedText extractedText = ic.getExtractedText(new ExtractedTextRequest(), 0);
int previousWordBoundary = getPreviousWordBoundary(extractedText.text, extractedText.selectionStart);
int start = extractedText.startOffset + previousWordBoundary;
int end = extractedText.startOffset + extractedText.selectionEnd;
ic.setSelection(start, end);
}
@Override
public void selectWordForward() {
InputConnection ic = getInputConnection();
if (ic == null) return;
ExtractedText extractedText = ic.getExtractedText(new ExtractedTextRequest(), 0);
int nextWordBoundary = getNextWordBoundary(extractedText.text, extractedText.selectionEnd);
int start = extractedText.startOffset + extractedText.selectionStart;
int end = extractedText.startOffset + nextWordBoundary;
ic.setSelection(start, end);
}
private boolean setCursor(InputConnection ic, int pos) {
if (mCurrentText == null) {
return false;
}
int textLen = mCurrentText.length();
pos = (pos < 0) ? 0 : ((pos <= textLen) ? pos : textLen);
int off = mExtractedText != null ? mExtractedText.startOffset : 0;
int cursor = off + pos;
return ic.setSelection(cursor, cursor);
}
/**
* Removes the word surrounding the cursor. Parameters are identical to
* getWordAtCursor.
*/
public static void deleteWordAtCursor(
InputConnection connection, String separators) {
Range range = getWordRangeAtCursor(connection, separators, null);
if (range == null) return;
connection.finishComposingText();
// Move cursor to beginning of word, to avoid crash when cursor is outside
// of valid range after deleting text.
int newCursor = getCursorPosition(connection) - range.charsBefore;
connection.setSelection(newCursor, newCursor);
connection.deleteSurroundingText(0, range.charsBefore + range.charsAfter);
}
/**
* Place the cursor on the next occurance of a symbol
*
* @param con
* driver
* @param symbol
* the symbol to jump to
*/
private void jumpForward(InputConnection con, int symbol) {
ExtractedText txt = con.getExtractedText(new ExtractedTextRequest(), 0);
if (txt != null) {
int pos = txt.text.toString().indexOf(symbol, txt.selectionEnd + 1);
if (pos == -1) {
pos = txt.text.length();
}
con.setSelection(pos, pos);
}
}
/**
* Place the cursor on the last occusrance of a symbol
*
* @param con
* driver
* @param symbol
* the symbol to jump to
*/
private void jumpBackward(InputConnection con, int symbol) {
ExtractedText txt = con.getExtractedText(new ExtractedTextRequest(), 0);
if (txt != null) {
int pos = txt.text.toString().lastIndexOf(symbol, txt.selectionEnd - 2);
pos++;
con.setSelection(pos, pos);
}
}
@Override
public void moveCursorStart() {
InputConnection ic = getInputConnection();
if (ic == null) return;
ic.setSelection(0, 0);
}