Java Code Examples for android.view.inputmethod.InputConnection#setSelection()

The following examples show how to use android.view.inputmethod.InputConnection#setSelection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 2
Source File: InputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
    }
}
 
Example 3
Source File: InputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
        }
    }
}
 
Example 4
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
@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);
}
 
Example 5
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
@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);
}
 
Example 6
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
@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);
}
 
Example 7
Source File: BrailleIME.java    From brailleback with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: EditingUtil.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: CtrlInputAction.java    From remotekeyboard with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 10
Source File: CtrlInputAction.java    From remotekeyboard with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
	}

}
 
Example 11
Source File: ImeContainer.java    From mongol-library with MIT License 4 votes vote down vote up
@Override
public void moveCursorStart() {
    InputConnection ic = getInputConnection();
    if (ic == null) return;
    ic.setSelection(0, 0);
}