android.view.inputmethod.InputConnection#getTextBeforeCursor ( )源码实例Demo

下面列出了android.view.inputmethod.InputConnection#getTextBeforeCursor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: mongol-library   文件: ImeContainer.java
/**
 * Returns the previous words before the cursor. If the cursor is touching a word then the text
 * from the cursor to the beginning of the word is counted as the first previous word.
 *
 * @param numberOfWords                the number of words to return
 * @param allowSingleSpaceBeforeCursor if true then a single space is ignored before the cursor
 *                                     and the word before that is counted as the first word
 * @return a array of words of length numberOfWords where index 0 is closest to the cursor
 */
public List<String> getPreviousMongolWords(int numberOfWords, boolean allowSingleSpaceBeforeCursor) {
    InputConnection ic = getInputConnection();
    List<String> words = new ArrayList<>();
    if (ic == null) return words;
    CharSequence previous = ic.getTextBeforeCursor(MAX_CHARS_BEFORE_CURSOR, 0);
    if (TextUtils.isEmpty(previous)) return words;

    int endIndex = previous.length();
    if (allowSingleSpaceBeforeCursor && isQualifiedSpaceAt(previous, endIndex - 1)) {
        endIndex--;
    }

    for (int i = 0; i < numberOfWords; i++) {
        int startIndex = getStartIndex(endIndex, previous);
        String word = previous.subSequence(startIndex, endIndex).toString();
        words.add(word);
        endIndex = startIndex;
        if (isQualifiedSpaceAt(previous, endIndex - 1)) {
            endIndex--;
        }
    }
    return words;
}
 
源代码2 项目: mongol-library   文件: ImeContainer.java
private boolean isSingleZwjThatSplitsWord(InputConnection ic, String text) {
    if (text.length() != 1
            || text.charAt(0) != MongolCode.Uni.ZWJ)
        return false;
    CharSequence previous = ic.getTextBeforeCursor(1, 0);
    if (TextUtils.isEmpty(previous))
        return false;
    char previousChar = previous.charAt(0);
    if (!MongolCode.isMongolian(previousChar)
            || previousChar == MongolCode.Uni.ZWJ)
        return false;
    CharSequence next = ic.getTextAfterCursor(1, 0);
    if (TextUtils.isEmpty(next))
        return false;
    char nextChar = next.charAt(0);
    return (MongolCode.isMongolian(nextChar)
            && nextChar != MongolCode.Uni.ZWJ);
}
 
源代码3 项目: hackerskeyboard   文件: LatinIME.java
private void swapPunctuationAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;
    CharSequence lastTwo = ic.getTextBeforeCursor(2, 0);
    if (lastTwo != null && lastTwo.length() == 2
            && lastTwo.charAt(0) == ASCII_SPACE
            && isSentenceSeparator(lastTwo.charAt(1))) {
        ic.beginBatchEdit();
        ic.deleteSurroundingText(2, 0);
        ic.commitText(lastTwo.charAt(1) + " ", 1);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo());
        mJustAddedAutoSpace = true;
    }
}
 
源代码4 项目: hackerskeyboard   文件: LatinIME.java
private void reswapPeriodAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;
    CharSequence lastThree = ic.getTextBeforeCursor(3, 0);
    if (lastThree != null && lastThree.length() == 3
            && lastThree.charAt(0) == ASCII_PERIOD
            && lastThree.charAt(1) == ASCII_SPACE
            && lastThree.charAt(2) == ASCII_PERIOD) {
        ic.beginBatchEdit();
        ic.deleteSurroundingText(3, 0);
        ic.commitText(" ..", 1);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo());
    }
}
 
源代码5 项目: hackerskeyboard   文件: LatinIME.java
private void doubleSpace() {
    // if (!mAutoPunctuate) return;
    if (mCorrectionMode == Suggest.CORRECTION_NONE)
        return;
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;
    CharSequence lastThree = ic.getTextBeforeCursor(3, 0);
    if (lastThree != null && lastThree.length() == 3
            && Character.isLetterOrDigit(lastThree.charAt(0))
            && lastThree.charAt(1) == ASCII_SPACE
            && lastThree.charAt(2) == ASCII_SPACE) {
        ic.beginBatchEdit();
        ic.deleteSurroundingText(2, 0);
        ic.commitText(". ", 1);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo());
        mJustAddedAutoSpace = true;
    }
}
 
源代码6 项目: hackerskeyboard   文件: LatinIME.java
private boolean isCursorTouchingWord() {
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return false;
    CharSequence toLeft = ic.getTextBeforeCursor(1, 0);
    CharSequence toRight = ic.getTextAfterCursor(1, 0);
    if (!TextUtils.isEmpty(toLeft) && !isWordSeparator(toLeft.charAt(0))
            && !isSuggestedPunctuation(toLeft.charAt(0))) {
        return true;
    }
    if (!TextUtils.isEmpty(toRight) && !isWordSeparator(toRight.charAt(0))
            && !isSuggestedPunctuation(toRight.charAt(0))) {
        return true;
    }
    return false;
}
 
源代码7 项目: hackerskeyboard   文件: LatinIME.java
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;
    }
}
 
源代码8 项目: hackerskeyboard   文件: EditingUtil.java
/**
 * 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);
}
 
源代码9 项目: hackerskeyboard   文件: EditingUtil.java
public static CharSequence getPreviousWord(InputConnection connection,
        String sentenceSeperators) {
    //TODO: Should fix this. This could be slow!
    CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
    if (prev == null) {
        return null;
    }
    String[] w = spaceRegex.split(prev);
    if (w.length >= 2 && w[w.length-2].length() > 0) {
        char lastChar = w[w.length-2].charAt(w[w.length-2].length() -1);
        if (sentenceSeperators.contains(String.valueOf(lastChar))) {
            return null;
        }
        return w[w.length-2];
    } else {
        return null;
    }
}
 
源代码10 项目: mongol-library   文件: ImeContainer.java
private char getPreviousChar() {
    InputConnection ic = getInputConnection();
    if (ic == null) return 0;
    CharSequence previous = ic.getTextBeforeCursor(1, 0);
    if (TextUtils.isEmpty(previous)) return 0;
    return previous.charAt(0);
}
 
源代码11 项目: mongol-library   文件: ImeContainer.java
private boolean needsHookedYForVowelYI(InputConnection ic, String text) {
    // XXX: Note that this makes it practically impossible for users to enter
    //      spellings like BAYINA for the double long tooth rendering
    // XXX: Developers can override this by subclassing ImeContainer
    //      and overriding commitText()
    if (text.length() != 1
            || text.charAt(0) != MongolCode.Uni.I)
        return false;
    CharSequence previous = ic.getTextBeforeCursor(2, 0);
    if (TextUtils.isEmpty(previous) || previous.length() != 2)
        return false;
    return (MongolCode.isVowel(previous.charAt(0))
            && previous.charAt(1) == MongolCode.Uni.YA);
}
 
源代码12 项目: mongol-library   文件: ImeContainer.java
private boolean shouldDoMvsShortcut(InputConnection ic, String text) {
    if (!text.equals(String.valueOf(MongolCode.Uni.A))
            && !text.equals(String.valueOf(MongolCode.Uni.E)))
        return false;
    CharSequence previousTwo = ic.getTextBeforeCursor(2, 0);
    return previousTwo != null
            && previousTwo.length() >= 2
            && MongolCode.isMvsPrecedingChar(previousTwo.charAt(0))
            && previousTwo.charAt(1) == text.charAt(0);
}
 
源代码13 项目: hackerskeyboard   文件: LatinIME.java
private void maybeRemovePreviousPeriod(CharSequence text) {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null || text.length() == 0)
        return;

    // When the text's first character is '.', remove the previous period
    // if there is one.
    CharSequence lastOne = ic.getTextBeforeCursor(1, 0);
    if (lastOne != null && lastOne.length() == 1
            && lastOne.charAt(0) == ASCII_PERIOD
            && text.charAt(0) == ASCII_PERIOD) {
        ic.deleteSurroundingText(1, 0);
    }
}
 
源代码14 项目: hackerskeyboard   文件: LatinIME.java
private void removeTrailingSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;

    CharSequence lastOne = ic.getTextBeforeCursor(1, 0);
    if (lastOne != null && lastOne.length() == 1
            && lastOne.charAt(0) == ASCII_SPACE) {
        ic.deleteSurroundingText(1, 0);
    }
}
 
源代码15 项目: hackerskeyboard   文件: EditingUtil.java
private static Range getWordRangeAtCursor(
        InputConnection connection, String sep, Range range) {
    if (connection == null || sep == null) {
        return null;
    }
    CharSequence before = connection.getTextBeforeCursor(1000, 0);
    CharSequence after = connection.getTextAfterCursor(1000, 0);
    if (before == null || after == null) {
        return null;
    }

    // Find first word separator before the cursor
    int start = before.length();
    while (start > 0 && !isWhitespace(before.charAt(start - 1), sep)) start--;

    // Find last word separator after the cursor
    int end = -1;
    while (++end < after.length() && !isWhitespace(after.charAt(end), sep));

    int cursor = getCursorPosition(connection);
    if (start >= 0 && cursor + end <= after.length() + before.length()) {
        String word = before.toString().substring(start, before.length())
                + after.toString().substring(0, end);

        Range returnRange = range != null? range : new Range();
        returnRange.charsBefore = before.length() - start;
        returnRange.charsAfter = end;
        returnRange.word = word;
        return returnRange;
    }

    return null;
}
 
源代码16 项目: hackerskeyboard   文件: LatinIME.java
private boolean sameAsTextBeforeCursor(InputConnection ic, CharSequence text) {
    CharSequence beforeText = ic.getTextBeforeCursor(text.length(), 0);
    return TextUtils.equals(text, beforeText);
}
 
源代码17 项目: mongol-library   文件: ImeContainer.java
/**
 * Keyboard.OnKeyboardListener method
 *
 * @param numberOfChars the number of characters located before the cursor
 *                      to request from the editor
 * @return the text before the cursor. The length may be shorter than requested.
 */
@Override
public CharSequence getTextBeforeCursor(int numberOfChars) {
    InputConnection ic = getInputConnection();
    if (ic == null) return "";
    return ic.getTextBeforeCursor(numberOfChars, 0);
}