导航抽屉中的Android打开AlertDialog

时间:2018-08-20 09:22:44

标签: android navigation-drawer alertdialog

我正在使用NavigationDrawer,菜单中有一个虚拟的Fragments和一个AlertDialog。当我单击以从抽屉中打开AlertDialog时,可以使用no按钮将其关闭,但是当我再次单击以打开它时,它并不想这样做。

然后我必须首先切换到抽屉中的其他片段之一,然后单击对话框将其打开。

如何在不先切换到另一个菜单项的情况下将其关闭后再次打开它。

MainActivity.java

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {

    switch (menuItem.getItemId()) {
        case R.id.drawer_fragment_one:
            if (!menuItem.isChecked()) {
                selectedIndex = COLLAPSING_TOOLBAR;
                menuItem.setChecked(true);
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new FragmentTabOne(), COLLAPSING_TOOLBAR_FRAGMENT_TAG).commit();
            }
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;

        case R.id.drawer_fragment_two:
            if (!menuItem.isChecked()) {
                selectedIndex = COLLAPSING_TOOLBAR;
                menuItem.setChecked(true);
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new FragmentTabTwo(), COLLAPSING_TOOLBAR_FRAGMENT_TAG).commit();
            }
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;

        case R.id.drawer_alert_dialog:
            if (!menuItem.isChecked()) {
                selectedIndex = ALERT_DIALOG;
                menuItem.setChecked(true);

                final AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setIcon(R.mipmap.ic_launcher);
                dialog.setTitle(R.string.app_name);
                dialog.setMessage(R.string.dialog_msg);
                dialog.setPositiveButton(R.string.dialog_option_yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

                dialog.setNegativeButton(R.string.dialog_option_no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });

                dialog.show();

            }
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;

        }
    return false;
}

2 个答案:

答案 0 :(得分:1)

我认为当您第一次单击处理设置为true的对话框的项目时,当您尝试第二次单击时测试是否未选中,这将返回false,结果您的对话框不会显示

答案 1 :(得分:0)

在以下代码段中:

julia> @macroexpand :(@permute_indices(foo, Dict(1=>2, 2=>3, 3=>1)))
  1. / (0.5 * h[i, j - 1, k] + h[i + 1, j, k])

添加以下行,希望它必须起作用:

case R.id.drawer_alert_dialog:
        if (!menuItem.isChecked()) {
            selectedIndex = ALERT_DIALOG;
            menuItem.setChecked(true);

            final AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
            dialog.setIcon(R.mipmap.ic_launcher);
            dialog.setTitle(R.string.app_name);
            dialog.setMessage(R.string.dialog_msg);
            dialog.setPositiveButton(R.string.dialog_option_yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });

            dialog.setNegativeButton(R.string.dialog_option_no, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                    .... (Add the line here)
                }
            });

            dialog.show();

        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;

在我提到的位置添加它(在此处添加行)