这篇文章讲述一下,长按文本的时候:选中文字、两个selection的图片如何加载的、复制粘贴(floatingtoolbar)如何显示的。
一切都是从textview开始的,在textview之前的就不分析了。
一、长按文本触发
长按文本,则必然是textview的performLongClick
@Override
public boolean performLongClick() {
boolean handled = false;
if (mEditor != null) {
mEditor.mIsBeingLongClicked = true;
}
if (super.performLongClick()) {
handled = true;
}
if (mEditor != null) {
handled |= mEditor.performLongClick(handled);//调用到editor的长按处理
mEditor.mIsBeingLongClicked = false;
}
二、核心处理:Editor中
public boolean performLongClick(boolean handled) {
// Long press in empty space moves cursor and starts the insertion action mode.根据注释,看到,这个if里面是处理:长按没有文本的空白地方的处理逻辑
if (!handled && !isPositionOnText(mLastDownPositionX, mLastDownPositionY) &&
mInsertionControllerEnabled) {//判断点击的位置(x,y)是否在text上
final int offset = mTextView.getOffsetForPosition(mLastDownPositionX,
mLastDownPositionY);//根据点击位置(x,y)获取offset位置,在Layout.java中的函数:getOffsetForHorizontal具体实现
Selection.setSelection((Spannable) mTextView.getText(), offset);//设置selection,关于selection不在详细讲述,这里没人改过,比较稳定。
getInsertionController().show();//这里就是显示图片,所调用code。---需要后需分析A.
mIsInsertionActionModeStartPending = true;
handled = true;
}
if (!handled && mTextActionMode != null) {//选中文本范围的逻辑
if (touchPositionIsInSelection()) {//是否有文本可以选中,有启动拖拽;这里也是见在左右两个范围图

本文详细介绍了Android中Textview长按操作触发的文本选中逻辑,包括选中文字的处理、两个selection的图片加载过程以及复制粘贴功能的浮动工具栏显示方法。一切起始于textview的performLongClick事件。

1万+

被折叠的 条评论
为什么被折叠?



