Android AlertDialog 在菜单选项中调用时不会显示

发布于 2024-10-31 06:54:24 字数 1846 浏览 0 评论 0原文

当调用菜单中的某个选项时,我试图调用 AlertDialog。用户选择的内容对于作为参数传递到网络上的 XML 页面非常重要。

我已尝试以下操作,但我的 AlertDialog 从未显示,它直接进入我的下一个活动。

@Override
    public boolean onOptionsItemSelected(MenuItem item) {    
        // Handle item selection    
        switch (item.getItemId()) {    
        case R.id.kantoor: 
            //Opent de office activity
            final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
            String arbeiderbed = pref.getString("ArbeiderBediende", "");

            if(arbeiderbed.equals("")){
                final CharSequence[] beroep = {"Arbeider","Bediende"};

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Kies uw statuut");
                builder.setSingleChoiceItems(beroep, -1, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        if (beroep[which].equals("Arbeider")){
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString("ArbeiderBediende", "arbeider");
                            editor.commit(); // Very important
                        }
                        else{
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString("ArbeiderBediende", "bediende");
                            editor.commit(); // Very important
                        }                           
                    }
                });
                builder.create().show();

            }
            Intent office = new Intent(MainActivity.this, Settings.class);
            startActivity(office);
            return true;

I'm trying to call an AlertDialog when a certain option in my menu is called. What the user chooses is important to pass as a parameter to an XML page on the net.

I've tried the following, but my AlertDialog never shows, it goes directly to my next Activity.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {    
        // Handle item selection    
        switch (item.getItemId()) {    
        case R.id.kantoor: 
            //Opent de office activity
            final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
            String arbeiderbed = pref.getString("ArbeiderBediende", "");

            if(arbeiderbed.equals("")){
                final CharSequence[] beroep = {"Arbeider","Bediende"};

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Kies uw statuut");
                builder.setSingleChoiceItems(beroep, -1, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        if (beroep[which].equals("Arbeider")){
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString("ArbeiderBediende", "arbeider");
                            editor.commit(); // Very important
                        }
                        else{
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString("ArbeiderBediende", "bediende");
                            editor.commit(); // Very important
                        }                           
                    }
                });
                builder.create().show();

            }
            Intent office = new Intent(MainActivity.this, Settings.class);
            startActivity(office);
            return true;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋顶上的小猫咪 2024-11-07 06:54:24

当然可以。警报对话框不是模态的,这意味着 show() 方法不会阻止进一步代码的执行。例如,如果您希望活动在对话框关闭时启动,则应将对 startActivity() 的调用放入对话框按钮的单击侦听器中。

Of course it does. Alert dialogs are not modal, meaning that the show() method does not block the execution of the further code. If you want the activity to start when the dialog closes, you should put the call to startActivity(), for instance, into the click listener of the dialog button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文