0

我有一个可选择的 TextView。我想在选择文本时阻止显示上下文菜单。

我实现了此代码,但仍显示操作栏和上下文菜单。

   tv.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Return true to ensure the yourTextView is still selectable
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // Remove all action items to provide an actionmode-less selection
            menu.clear();
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {

        }
    });

怎么可能永远删除?

4

2 回答 2

0
@Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            menu.removeItem(android.R.id.selectAll);
            menu.removeItem(android.R.id.cut);
            menu.removeItem(android.R.id.copy);
            menu.removeItem(android.R.id.paste);
            menu.removeItem(android.R.id.replaceText);
            menu.removeItem(android.R.id.shareText);
            return true;
        }

尝试从菜单中手动删除项目。我希望它现在应该工作。

于 2020-05-30T13:19:44.333 回答
0

我想了想,最后写了如下代码:

    tv.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            removeActionBar(getWindow().getDecorView());
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {

        }
    });

    private void removeActionBar(View v) {
            ViewGroup viewgroup = (ViewGroup) v;

            for (int i = 0; i < viewgroup.getChildCount(); i++) {
                    View v1=viewgroup.getChildAt(i);
                    if ( v1 instanceof ViewGroup )
                            removeActionBar(v1);
                    if ( v1 instanceof ActionBarContextView ) {
                            viewgroup.removeView(v1);
                    }
            }
    }
于 2020-05-30T23:09:16.060 回答