wait to trigger action

偶尔善良 提交于 2020-01-06 17:18:31

问题


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:


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.



来源:https://stackoverflow.com/questions/6879656/wait-to-trigger-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!