问题
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