等待触发动作
我希望触发一个 while 循环,但仅在用户选择一个选项之后。现在由于某种原因,即使在用户选择一个选项之前,它也会自动遍历整个代码块。我如何强制它等待继续,直到用户选择某些内容?
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 while 循环放在 onClick 或 onClick 调用的方法中。
builderPC.create().show();
不会阻止或执行任何操作来等待响应,但 onclick 是由用户响应触发的。请记住这将在主线程上运行。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.