有没有人知道Android EditText中“setSelection”的源代码?

时间:2013-05-31 03:09:07

标签: android android-edittext selection

enter image description here

我想在其中使用ImageSpan突出显示EditText的一部分,但BackgroundSpan对ImageSpan没有任何影响。我想到了函数:setSelection。我想高亮显示EditText但不要选择。

我想我可以从setSelection的源代码中学习,有人可以帮助我或向我提供setSelection的源代码吗?enter image description here

1 个答案:

答案 0 :(得分:0)

这些是源代码

  /**
     * Set the selection anchor to <code>start</code> and the selection edge
     * to <code>stop</code>.
     */
    public static void setSelection(Spannable text, int start, int stop) {
        // int len = text.length();
        // start = pin(start, 0, len);  XXX remove unless we really need it
        // stop = pin(stop, 0, len);

        int ostart = getSelectionStart(text);
        int oend = getSelectionEnd(text);

        if (ostart != start || oend != stop) {
            text.setSpan(SELECTION_START, start, start,
                         Spanned.SPAN_POINT_POINT|Spanned.SPAN_INTERMEDIATE);
            text.setSpan(SELECTION_END, stop, stop,
                         Spanned.SPAN_POINT_POINT);
        }
    }



   /**
     * Move the cursor to offset <code>index</code>.
     */
    public static final void setSelection(Spannable text, int index) {
        setSelection(text, index, index);
    }

查看完整来源here