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

android-菜单按钮只能被单击一次,第1张

概述我使用以下代码将自定义菜单添加到菜单按钮:@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); if (getDisplayedView() instanceof WorkspaceView)

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

@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)

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

在活动中,有以下代码:

@OverrIDepublic 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()));            }        }    }}
总结

以上是内存溢出为你收集整理的android-菜单按钮只能被单击一次 全部内容,希望文章能够帮你解决android-菜单按钮只能被单击一次 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145209.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存