文本长按选中逻辑

这篇文章讲述一下,长按文本的时候:选中文字、两个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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值