Remove default CAB items in TextView selection ActionMode

℡╲_俬逩灬. 提交于 2019-12-08 11:21:21

问题


I'm trying to remove default items, such as "Copy" and "Select All", from the CAB when the user selects some text from a TextView element.

I've overridden the default action using this code, but inflating a new menu resource doesn't remove default items.

textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {

        mode.setTitle("Test");
        mode.getMenuInflater().inflate(R.menu.new_menu_selection, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {

        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {

    }
});

Is there a solution to this?


回答1:


To remove the default items you can call menu.clear() before inflate the new menu:

textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {

        menu.clear();

        mode.setTitle("Test");
        mode.getMenuInflater().inflate(R.menu.new_menu_selection, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {

        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {

    }
});


来源:https://stackoverflow.com/questions/33597729/remove-default-cab-items-in-textview-selection-actionmode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!