等待触发动作 [英] wait to trigger action

查看:105
本文介绍了等待触发动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待触发while循环,但只有用户选择后一个选项。现在某些原因,它会自动通过code的整个块吹用户选取一个选项,甚至在。我怎样才能迫使它等待要持续到用户选择的东西吗?

 情况下R.id.buttonSetPlayers:
        // // **********************
        // *** SET PLAYER COUNT *** //
        // // **********************
        AlertDialog.Builder builderPC =新AlertDialog.Builder(本);
        最终的CharSequence [] playerCount = {1,2,3,4};
        builderPC.setTitle(播放计数);
        builderPC.setItems(playerCount,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface dialogInterface,诠释项){
                Toast.makeText(getApplicationContext(),playerCount [项目],Toast.LENGTH_SHORT).show();
                如果(playerCount [项目] .equals(1)){
                    EntryScreen.this.totalPlayerCount = 1;                }
                否则如果(playerCount [项目] .equals(2)){
                    EntryScreen.this.totalPlayerCount = 2;                }
                否则如果(playerCount [项目] .equals(3)){
                    EntryScreen.this.totalPlayerCount = 3;                }
                否则如果(playerCount [项目] .equals(4)){
                    EntryScreen.this.totalPlayerCount = 4;                }
                返回;
            }
        });
        builderPC.create()显示()。
###我希望它等待尽快按钮被点击做到这一点在下一部分,而不是这样做的。下面...###
        而(totalPlayerCount> = 1){
            setNames();
            totalPlayerCount--;
        }        返回;


解决方案

放入的onClick while循环或通过的onClick调用的方法。 builderPC.create()显示(); 不会阻止或做任何事情来等待响应,但的onclick由用户响应触发时。请记住,这将在主线程上运行。

I am looking to trigger a while loop, but only after the user selects an option. For some reason now, it is automatically blowing through the entire block of code even before the user picks an option. How can I force it to wait to continue until a user selects something?

case R.id.buttonSetPlayers:
        //**********************//
        //***SET PLAYER COUNT***//
        //**********************//
        AlertDialog.Builder builderPC = new AlertDialog.Builder(this);
        final CharSequence[] playerCount = {"1", "2", "3", "4"};
        builderPC.setTitle("Player Count");
        builderPC.setItems(playerCount, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
                Toast.makeText(getApplicationContext(), playerCount[item], Toast.LENGTH_SHORT).show();
                if (playerCount[item].equals("1")){
                    EntryScreen.this.totalPlayerCount = 1;

                }
                else if (playerCount[item].equals("2")){
                    EntryScreen.this.totalPlayerCount = 2;

                }
                else if (playerCount[item].equals("3")){
                    EntryScreen.this.totalPlayerCount = 3;

                }
                else if (playerCount[item].equals("4")){
                    EntryScreen.this.totalPlayerCount = 4;

                }
                return;
            }
        });
        builderPC.create().show();


###

I want it to wait to do this next part instead of doing it as soon as button is clicked.  Below...

###
        while (totalPlayerCount >= 1){
            setNames();
            totalPlayerCount--;
        }

        return;

解决方案

Put the while loop in onClick or a method called by onClick. builderPC.create().show(); does not block or do anything to wait for the response but onclick is trigged by the user response. Just remember this will run on the main thread.

这篇关于等待触发动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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