【问题标题】:wait to trigger action等待触发动作
【发布时间】:2011-07-29 22:28:26
【问题描述】:

我希望触发一个 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;

【问题讨论】:

  • 我觉得这个问题有点不清楚,你是在等待builderPC.create().show(); 阻止吗?
  • 抱歉,我的评论下方的 while 循环是我不想触发的,直到用户选择了 1-4。然后我需要这个过程循环很多次。

标签: android loops button while-loop option


【解决方案1】:

将while循环放在onClick或onClick调用的方法中。 builderPC.create().show(); 不会阻止或做任何事情来等待响应,但 onclick 会由用户响应触发。请记住,这将在主线程上运行。

【讨论】:

  • 谢谢...不知道为什么我以前没有考虑过这个问题。我只是在 setNames() 中创建了一个 while 循环,然后在每个 if 语句中触发它,所以一旦选择它就会通过 setNames 运行一定的次数。
猜你喜欢
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 2020-11-05
  • 2015-02-20
  • 2021-11-01
  • 2019-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多