Search in sources :

Example 46 with Editable

use of android.text.Editable in project platform_frameworks_base by android.

the class BaseInputConnection method replaceText.

private void replaceText(CharSequence text, int newCursorPosition, boolean composing) {
    final Editable content = getEditable();
    if (content == null) {
        return;
    }
    beginBatchEdit();
    // delete composing text set previously.
    int a = getComposingSpanStart(content);
    int b = getComposingSpanEnd(content);
    if (DEBUG)
        Log.v(TAG, "Composing span: " + a + " to " + b);
    if (b < a) {
        int tmp = a;
        a = b;
        b = tmp;
    }
    if (a != -1 && b != -1) {
        removeComposingSpans(content);
    } else {
        a = Selection.getSelectionStart(content);
        b = Selection.getSelectionEnd(content);
        if (a < 0)
            a = 0;
        if (b < 0)
            b = 0;
        if (b < a) {
            int tmp = a;
            a = b;
            b = tmp;
        }
    }
    if (composing) {
        Spannable sp = null;
        if (!(text instanceof Spannable)) {
            sp = new SpannableStringBuilder(text);
            text = sp;
            ensureDefaultComposingSpans();
            if (mDefaultComposingSpans != null) {
                for (int i = 0; i < mDefaultComposingSpans.length; ++i) {
                    sp.setSpan(mDefaultComposingSpans[i], 0, sp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
                }
            }
        } else {
            sp = (Spannable) text;
        }
        setComposingSpans(sp);
    }
    if (DEBUG)
        Log.v(TAG, "Replacing from " + a + " to " + b + " with \"" + text + "\", composing=" + composing + ", type=" + text.getClass().getCanonicalName());
    if (DEBUG) {
        LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG);
        lp.println("Current text:");
        TextUtils.dumpSpans(content, lp, "  ");
        lp.println("Composing text:");
        TextUtils.dumpSpans(text, lp, "  ");
    }
    // we are providing here.
    if (newCursorPosition > 0) {
        newCursorPosition += b - 1;
    } else {
        newCursorPosition += a;
    }
    if (newCursorPosition < 0)
        newCursorPosition = 0;
    if (newCursorPosition > content.length())
        newCursorPosition = content.length();
    Selection.setSelection(content, newCursorPosition);
    content.replace(a, b, text);
    if (DEBUG) {
        LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG);
        lp.println("Final text:");
        TextUtils.dumpSpans(content, lp, "  ");
    }
    endBatchEdit();
}
Also used : Editable(android.text.Editable) Spannable(android.text.Spannable) SpannableStringBuilder(android.text.SpannableStringBuilder) LogPrinter(android.util.LogPrinter)

Example 47 with Editable

use of android.text.Editable in project platform_frameworks_base by android.

the class BaseInputConnection method getSelectedText.

/**
     * The default implementation returns the text currently selected, or null if none is
     * selected.
     */
public CharSequence getSelectedText(int flags) {
    final Editable content = getEditable();
    if (content == null)
        return null;
    int a = Selection.getSelectionStart(content);
    int b = Selection.getSelectionEnd(content);
    if (a > b) {
        int tmp = a;
        a = b;
        b = tmp;
    }
    if (a == b)
        return null;
    if ((flags & GET_TEXT_WITH_STYLES) != 0) {
        return content.subSequence(a, b);
    }
    return TextUtils.substring(content, a, b);
}
Also used : Editable(android.text.Editable)

Example 48 with Editable

use of android.text.Editable in project platform_frameworks_base by android.

the class BaseInputConnection method clearMetaKeyStates.

/**
     * Default implementation uses
     * {@link MetaKeyKeyListener#clearMetaKeyState(long, int)
     * MetaKeyKeyListener.clearMetaKeyState(long, int)} to clear the state.
     */
public boolean clearMetaKeyStates(int states) {
    final Editable content = getEditable();
    if (content == null)
        return false;
    MetaKeyKeyListener.clearMetaKeyState(content, states);
    return true;
}
Also used : Editable(android.text.Editable)

Example 49 with Editable

use of android.text.Editable in project platform_frameworks_base by android.

the class BaseInputConnection method getTextBeforeCursor.

/**
     * The default implementation returns the given amount of text from the
     * current cursor position in the buffer.
     */
public CharSequence getTextBeforeCursor(int length, int flags) {
    final Editable content = getEditable();
    if (content == null)
        return null;
    int a = Selection.getSelectionStart(content);
    int b = Selection.getSelectionEnd(content);
    if (a > b) {
        int tmp = a;
        a = b;
        b = tmp;
    }
    if (a <= 0) {
        return "";
    }
    if (length > a) {
        length = a;
    }
    if ((flags & GET_TEXT_WITH_STYLES) != 0) {
        return content.subSequence(a - length, a);
    }
    return TextUtils.substring(content, a - length, a);
}
Also used : Editable(android.text.Editable)

Example 50 with Editable

use of android.text.Editable in project platform_frameworks_base by android.

the class BaseInputConnection method setComposingRegion.

public boolean setComposingRegion(int start, int end) {
    final Editable content = getEditable();
    if (content != null) {
        beginBatchEdit();
        removeComposingSpans(content);
        int a = start;
        int b = end;
        if (a > b) {
            int tmp = a;
            a = b;
            b = tmp;
        }
        // Clip the end points to be within the content bounds.
        final int length = content.length();
        if (a < 0)
            a = 0;
        if (b < 0)
            b = 0;
        if (a > length)
            a = length;
        if (b > length)
            b = length;
        ensureDefaultComposingSpans();
        if (mDefaultComposingSpans != null) {
            for (int i = 0; i < mDefaultComposingSpans.length; ++i) {
                content.setSpan(mDefaultComposingSpans[i], a, b, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
            }
        }
        content.setSpan(COMPOSING, a, b, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
        // Note: sendCurrentText does nothing unless mDummyMode is set
        sendCurrentText();
        endBatchEdit();
    }
    return true;
}
Also used : Editable(android.text.Editable)

Aggregations

Editable (android.text.Editable)511 TextWatcher (android.text.TextWatcher)158 View (android.view.View)117 TextView (android.widget.TextView)77 Paint (android.graphics.Paint)73 Spannable (android.text.Spannable)45 TextPaint (android.text.TextPaint)44 EditText (android.widget.EditText)39 KeyEvent (android.view.KeyEvent)34 InputMethodManager (android.view.inputmethod.InputMethodManager)33 Intent (android.content.Intent)30 ImageView (android.widget.ImageView)30 SpellCheckSpan (android.text.style.SpellCheckSpan)26 SuppressLint (android.annotation.SuppressLint)24 AdapterView (android.widget.AdapterView)23 Spanned (android.text.Spanned)22 SuggestionSpan (android.text.style.SuggestionSpan)21 ListView (android.widget.ListView)19 SpannableStringBuilder (android.text.SpannableStringBuilder)18 DialogInterface (android.content.DialogInterface)16