wait to trigger action

2019-09-19 02:23发布

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;

1条回答
We Are One
2楼-- · 2019-09-19 03:23

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.

查看更多
登录 后发表回答