textIsSelectable

首先是文字可选,需要添加如下的属性
android:textIsSelectable="true"
edittext不能加这个属性,默认就是可选择的,加了以后无法编辑了。
系统默认的可能长这样

image.png

如果要修改这个弹框咋办?
需要给这个控件通过方法setCustomSelectionActionModeCallback设置如下回调,自己来处理
先看下这个方法的注释,注释里默认的item的id,如果你想单独删除哪个,可以通过menu的remove方法删掉

    /**
     * If provided, this ActionMode.Callback will be used to create the ActionMode when text
     * selection is initiated in this View.
     *
     * 

The standard implementation populates the menu with a subset of Select All, Cut, Copy, * Paste, Replace and Share actions, depending on what this View supports. * *

A custom implementation can add new entries in the default menu in its * {@link android.view.ActionMode.Callback#onPrepareActionMode(ActionMode, android.view.Menu)} * method. The default actions can also be removed from the menu using * {@link android.view.Menu#removeItem(int)} and passing {@link android.R.id#selectAll}, * {@link android.R.id#cut}, {@link android.R.id#copy}, {@link android.R.id#paste}, * {@link android.R.id#replaceText} or {@link android.R.id#shareText} ids as parameters. * *

Returning false from * {@link android.view.ActionMode.Callback#onCreateActionMode(ActionMode, android.view.Menu)} * will prevent the action mode from being started. * *

Action click events should be handled by the custom implementation of * {@link android.view.ActionMode.Callback#onActionItemClicked(ActionMode, * android.view.MenuItem)}. * *

Note that text selection mode is not started when a TextView receives focus and the * {@link android.R.attr#selectAllOnFocus} flag has been set. The content is highlighted in * that case, to allow for quick replacement. */ public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) { createEditorIfNeeded(); mEditor.mCustomSelectionActionModeCallback = actionModeCallback; }

我们的简单实现

//menu文件夹下弄个menu的资源文件select_menu2.xml


    
    


            et_1.customSelectionActionModeCallback = object : ActionMode.Callback2() {
                override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
                    println("2==============onActionItemClicked===" + item.title)
                    if (item.itemId==android.R.id.selectAll) {
                        mode.invalidate() // 这样重新弹框,完事全选就没了,因为这时候已经是全选了。
                    } else {
                        mode.finish() //关闭弹框
                    }
                    return false
                }
                //长按文字这个方法最先执行
                override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
                    println("2==============onCreateActionMode===" + menu.size())
                    menu.clear()
                    mode.menuInflater.inflate(R.menu.select_menu2, menu)
                    return true //返回false就不会弹框了。。
                }
              //然后会执行这个方法,这个方法会执行5次。。
                override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
                    println("2==============onPrepared===" + menu.size())
                  //上边的select_menu2里边只有2个,可这里打印的是3个,下边分析原因。
              //  如果系统提供的一个都不要,那么把onCreateActionMode的代码写在这里
                    return true
                }

                override fun onDestroyActionMode(mode: ActionMode) {
                    println("2==============onDestroyActionMode===" + mode.title)
                }
            }

下边分析下为啥,上边的回调都是在一个Editor的类里调用的,如下边
Editor里有个private class TextActionModeCallback extends ActionMode.Callback2的类。

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            updateSelectAllItem(menu);
            updateReplaceItem(menu);
            updateAssistMenuItem(menu);
            //上边3行代码,就是系统根据条件加上了这3个,所以我们在onCreateActionMode里明明清空之后加了2个,
    //为啥到这里又多了一个。
            Callback customCallback = getCustomCallback();
            if (customCallback != null) {
                return customCallback.onPrepareActionMode(mode, menu);
            }
            return true;
        }

另外默认的选项的点击事件可以在textview里下边的方法里查到

return mTextView.onTextContextMenuItem(item.getItemId());

你可能感兴趣的:(textIsSelectable)