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类很简单,包含了多个静态方法
获取选择区域的起(终)点或者游标位置,如果没有选择区域或者游标,则返回-1
设置选择区域或者游标位置,最终都是通过调用
setSelection(Spannable text, int start, int stop)
方法实现的
扩展选择区域,index为偏移量
移除所有Selection
移动游标的系列方法,参数text我们可以通过EditText中的getText()获得,layout可以通过EditText中的getLayout()获得
扩展游标的系列方法
其实以上所有与游标操作有关系的函数最终都是通过调用以下两个方法实现的
再看这两个方法,其实最终的实现就是setSpan方法。
其中还有两个静态变量Selection.SELECTION_START和Selection.SELECTION_END
这两个对象就是选择区域起点与终点的标志。分别为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对象中。
引用一段安卓源码中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对象中。
相关文章推荐
- 爬虫Larbin解析(一)——Larbin配置与使用
- 2014扬声器的信息中国建筑师大会
- 第 4 章 Android 应用的调试
- pat(B)1009. 说反话(逆迭代器)
- MySQL 主键与索引的联系与区别分析
- 接口隔离原则(ISP)
- 栈的应用1——超级计算器(中缀与后缀表达式)C语言
- jquery实现很酷的网页顶部图标下拉菜单效果
- Excel一项研究指出色彩
- 【LeetCode】264. Ugly Number II
- java获取list集合转换成json数据
- hbase0.94.14+hadoop2.6.0
- 第2期Spark纯实战公益大讲坛:通过案例实战掌握Spark编程模型内幕
- ubuntu中,如何把安装的软件启动图标加到桌面上
- web开发常用正则表达式
- 在Linux系统中chowna+x 与chmod o+x有什么区别
- LayoutInflater.inflate方法解析
- 输入法隐藏、弹出
- jqgrid控件列分组
- 绘制圆角的 LinearLayout