android-菜单按钮只能被单击一次

前端之家收集整理的这篇文章主要介绍了android-菜单按钮只能被单击一次 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我使用以下代码自定义菜单添加菜单按钮:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        if (getDisplayedView() instanceof WorkspaceView) {
            ((WorkspaceView) getDisplayedView()).showEditMenu();
        }
        return true;
    }

public void showEditMenu() {
        new EditMenu(lexs,((Project) projects.getSelectedItem()).getName(),((ProjectList) projectsList.getSelectedItem()).getName()).show();
    }

EditMenu通过以下方式实现:

public class EditMenu {

    private final String DELETE_PROJECT = "Projekt löschen";
    private final String DELETE_LIST = "Liste löschen";
    private final String RENAME_PROJECT = "Projekt umbenennen";
    private final String RENAME_LIST = "Liste umbenennen";
    private final String CLOSE = "Menü schliessen";

    private Context context;
    private String projectName;
    private String listName;
    private AlertDialog alert;

    private final CharSequence[] items = {DELETE_PROJECT,DELETE_LIST,RENAME_PROJECT,RENAME_LIST,CLOSE};

    public EditMenu(Context context,String projectName,String listName) {
        this.context = context;
        this.projectName = projectName;
        this.listName = listName;
    }

    public void show() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(projectName + ": " + listName);
        builder.setItems(items,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int item) {
                if (items[item].equals(DELETE_PROJECT)) {
                    deleteProject();
                } else if (items[item].equals(DELETE_LIST)) {
                    deleteList();
                } else if (items[item].equals(RENAME_PROJECT)) {
                    renameProject();
                } else if (items[item].equals(RENAME_LIST)) {
                    renameList();
                } else if (items[item].equals(CLOSE)) {
                    close();
                }
            }
        });
        alert = builder.create();
        alert.show();
    }

    private void deleteProject() {

    }

    private void deleteList() {

    }

    private void renameProject() {

    }

    private void renameList() {

    }

    private void close() {

    }
}

如果我第一次单击菜单按钮,这将正常工作.但是,如果上下文菜单关闭并且我第二次单击菜单按钮,则什么也没有发生.

我也试过打电话

方法close()中使用alert.close(),alert.hide(),alert.dismiss()等,但是并不能改善这种情况.有什么提示吗?谢谢!

最佳答案
由于将近3周没有其他答案,因此我将自己回答问题:

而不是覆盖

public boolean onCreateOptionsMenu(Menu menu)

一个必须重写

public boolean onPrepareOptionsMenu(Menu menu)

这是一个简短的示例,该如何做:

在活动中,有以下代码

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        showMenu();
        return true;
}

private void showMenu() {
        EditMenu menu = new EditMenu(this,"Pacman Menu");
        menu.show();
}

然后,clsas EditMenu看起来如下所示:

public class EditMenu {

    private final String QUIT = "Quit";
    private final String RESTART = "New Game";
    private final String SOUND = "Switch Sound";
    private final String PAUSE = "Un/pause";
    private final CharSequence[] items = new CharSequence[] {QUIT,RESTART,SOUND,PAUSE};

    private Context context;
    private String title;
    private AlertDialog alert;
    private MenuListener listener = new MenuListener();

    public EditMenu(Context context,String title) {
        this.context = context;
        this.title = title;
    }

    public void show() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.drawable.splashscreen);
        builder.setTitle(title);
        builder.setItems(items,listener);
        alert = builder.create();
        alert.show();
    }

    private class MenuListener implements DialogInterface.OnClickListener {

        @Override
        public void onClick(DialogInterface dialog,int item) {
            if (items[item].equals(QUIT)) {
                ((PacmanGame) context).quitGame();
            } else if (items[item].equals(RESTART)) {
                ((PacmanGame) context).restart();
            } else if (items[item].equals(SOUND)) {
                Sound.setSoundOn(! Sound.isSoundOn());
            } else if (items[item].equals(PAUSE)) {
                ((PacmanGame) context).getGameBoard().setPausing(!(((PacmanGame) context).getGameBoard().isPaused()));
            }
        }

    }
}
原文链接:https://www.f2er.com/android/531519.html

猜你在找的Android相关文章