且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

等待触发动作

更新时间:2022-10-15 09:06:20

放入的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.