这篇文章讲述一下,长按文本的时候:选中文字、两个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 != n