在DialogFragment中选择文本时,如何禁用操作栏? [英] How can I disable the action bar when text is selected in a DialogFragment?

查看:72
本文介绍了在DialogFragment中选择文本时,如何禁用操作栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发ICS(4.0.3)平板电脑应用,其中我们广泛使用 DialogFrament .当用户在对话框片段中的文本编辑器内长按时,我需要防止复制/粘贴操作栏出现在屏幕顶部.幸运的是,我们有自己的基类,扩展了我们所有对话框片段所基于的 DialogFragment (称为 MyOrgDialogFragment ),因此我可以根据需要进行修改.

I'm working on an ICS (4.0.3) tablet app where we're using DialogFrament extensively. When the user does a long press inside a text editor in a dialog fragment I need to prevent the copy/paste action bar from appearing at the top of the screen. Fortunately we have our own base class extending DialogFragment (call it MyOrgDialogFragment) that all our dialog fragments are based on, so I can modify that if required.

我的第一次尝试涉及覆盖 MyOrgDialogFragment 中的 OnCreateDialog(),然后覆盖其中的 onWindowStartingActionMode(),即:

My first attempt has involved overriding OnCreateDialog() in MyOrgDialogFragment, then overriding onWindowStartingActionMode() inside that, i.e.:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    return new Dialog(getActivity()) {
        @Override
        public ActionMode onWindowStartingActionMode(ActionMode.Callback callback)
        {
            return new ActionMode()
            {
                // [ All overrides empty ]
            }
        }
    };
}

这几乎起作用:它阻止了操作栏的出现,并且当我在编辑字段中长按时选择手柄仍然出现(这很好),但是当我将焦点移到上时,选择手柄无法正确关闭另一个编辑字段.最后,我将僵尸"选择手柄悬停在我在其中进行选择的每个编辑字段上.

This nearly works: it prevents the action bar from appearing, and the selection handles still appear when I long-press in an edit field (which is fine), but the selection handles aren't dismissed correctly when I move focus onto another edit field. I end up with "zombie" selection handles hovering over each edit field I've made a selection in.

任何人都可以指出我想要更好的解决方案吗?

Can anyone point me towards a better solution?

推荐答案

看看

Have a look at the setCustomSelectionActionModeCallback method that can be used on a TextView/EditText. If you use an empty ActionMode.Callback like this:

private ActionMode.Callback mModeCallback = new Callback() {

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // TODO Auto-generated method stub
            return false;
        }
    };

选择 ActionMode 将被阻止(切换字段时没有选择处理程序剩余,实际上它们根本不会出现).

the selection ActionMode will be blocked(with no selection handlers remaining when switching fields, in fact they will not appear at all).

这篇关于在DialogFragment中选择文本时,如何禁用操作栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆