您的位置:首页 > 其它

Selection使用简介

2015-08-22 12:16 405 查看
Utility class for manipulating cursors and selections in CharSequences. A cursor is a selection where the start and end are at the same offset.

引用一段安卓源码中Selection类的注释,Selection是一个在 CharSequences中操纵游标和”选择”(不知道怎么翻译好。。)的一个工具类。游标就是起点和终点在同一位置的”选择”。

EditText中setSelection系列方法就是通过Selection实现的。

Selection类很简单,包含了多个静态方法

public static final int getSelectionStart(CharSequence text)
public static final int getSelectionEnd(CharSequence text)


获取选择区域的起(终)点或者游标位置,如果没有选择区域或者游标,则返回-1

public static final void setSelection(Spannable text, int index)
public static final void selectAll(Spannable text)
public static void setSelection(Spannable text, int start, int stop)


设置选择区域或者游标位置,最终都是通过调用

setSelection(Spannable text, int start, int stop)

方法实现的

public static final void extendSelection(Spannable text, int index)


扩展选择区域,index为偏移量

public static final void removeSelection(Spannable text)


移除所有Selection

public static boolean moveUp(Spannable text, Layout layout)
public static boolean moveDown(Spannable text, Layout layout)
public static boolean moveLeft(Spannable text, Layout layout)
public static boolean moveRight(Spannable text, Layout layout)
public static boolean moveToLeftEdge(Spannable text, Layout layout)
public static boolean moveToRightEdge(Spannable text, Layout layout)


移动游标的系列方法,参数text我们可以通过EditText中的getText()获得,layout可以通过EditText中的getLayout()获得

public static boolean extendUp(Spannable text, Layout layout)
public static boolean extendDown(Spannable text, Layout layout)
public static boolean extendLeft(Spannable text, Layout layout)
public static boolean extendRight(Spannable text, Layout layout)
public static boolean extendToLeftEdge(Spannable text, Layout layout)
public static boolean extendToRightEdge(Spannable text, Layout layout)


扩展游标的系列方法

其实以上所有与游标操作有关系的函数最终都是通过调用以下两个方法实现的

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);
}
}
public static final void extendSelection(Spannable text, int index) {
if (text.getSpanStart(SELECTION_END) != index)
text.setSpan(SELECTION_END, index, index, Spanned.SPAN_POINT_POINT);
}


再看这两个方法,其实最终的实现就是setSpan方法。

其中还有两个静态变量Selection.SELECTION_START和Selection.SELECTION_END

public static final Object SELECTION_START = new START();
public static final Object SELECTION_END = new END();

private static final class START implements NoCopySpan { }
private static final class END implements NoCopySpan { }


这两个对象就是选择区域起点与终点的标志。分别为START和END的实例,但是都继承自NoCopySpan。而NoCopySpan没有任何函数,只有一个内部类。我们看看代码注释。

This interface should be added to a span object that should not be copied into a new Spenned when performing a slice or copy operation on the original Spanned it was placed in.

大致意思是继承了该接口的span对象不应该被拷贝或者复制到另一个Spanned对象中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: