0

我的应用程序有几个按钮,它们上面的文本是一个计数器。每个按钮都有不同的计数器。我想独立地开始我想要的2。我的问题是,当我按下一个按钮时,所有其他按钮计数器都会启动。所以我有很多按钮可以在按下一个时启动计数器。我知道有很多大声笑应用程序,但我想练习编码 android:PI 知道问题出在 onTick 方法上。但我不知道如何为每个按钮调用多个 onTick 方法,或者在 onTick 中有 if/switch 语句。任何人都知道我该怎么做?

谢谢你的时间

public class TimersActivity extends Activity {

Button wraith, wolf, golem, blue_golem, red_golem, enemy_wraith, enemy_wolf, enemy_golem, enemy_blue_golem, enemy_red_golem, dragon, baron;

final MyCounter wraithTimer = new MyCounter(50000,100, wraith);
final MyCounter enemy_wraithTimer = new MyCounter(50000,100, wolf);

final MyCounter wolfTimer = new MyCounter(60000,100, null);
final MyCounter enemy_wolfTimer = new MyCounter(60000,100, null);
final MyCounter golemTimer = new MyCounter(60000,100, null);
final MyCounter enemy_golemTimer = new MyCounter(60000,100, null);
final MyCounter blue_golemTimer = new MyCounter(300000,100, null);
final MyCounter enemy_blue_golemTimer = new MyCounter(300000,100, null);
final MyCounter red_golemTimer = new MyCounter(300000,100, null);
final MyCounter enemy_red_golemTimer = new MyCounter(300000,100, null);
final MyCounter dragonTimer = new MyCounter(360000,100, null);
final MyCounter baronTimer = new MyCounter(420000,100, null);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timers);

        wraith = (Button)findViewById(R.id.wraiths);
        wolf = (Button)findViewById(R.id.wolfs);
        golem = (Button)findViewById(R.id.golems);
        blue_golem = (Button)findViewById(R.id.blue_golem);
        red_golem = (Button)findViewById(R.id.redbuff_creep);
        enemy_wraith = (Button)findViewById(R.id.enemy_wraiths);
        enemy_wolf = (Button)findViewById(R.id.enemy_wolfs);
        enemy_golem = (Button)findViewById(R.id.enemy_golems);
        enemy_blue_golem = (Button)findViewById(R.id.enemy_blue_golem);
        enemy_red_golem = (Button)findViewById(R.id.enemy_redbuff_creep);
        dragon = (Button)findViewById(R.id.dragon);
        baron = (Button)findViewById(R.id.baron);

        wraith.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                wraithTimer.start();
            }
        });

    }

    public enum buttons{wraith,wolf};
    public class MyCounter extends CountDownTimer{



        public MyCounter(long millisInFuture, long countDownInterval, Button nam) {
            super(millisInFuture, countDownInterval);
        }


        public void onFinish() {
            wraith.setText("DONE");

        }
        public void onTick(long millisUntilFinished, buttons names) {

            switch(names){

            case wraith: 
                wraith.setText((millisUntilFinished/1000)+"");
                break;
            case wolf: 
                 wolf.setText((millisUntilFinished/1000)+"");
                 break;         
            }

        }


        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub

        }

    }

}
4

1 回答 1

1

一种选择是在 MyCounter 类中使用 Enums。

只需在构造函数中分配它,然后在 onTick() 方法中,使用带有枚举作为参数的开关。


编辑:

在构造函数中使用枚举意味着在初始化程序中使用此代码:

final MyCounter <name>Timer = new MyCounter(x0000, 100, buttons.wraith);

在您的 MyCounter 类中添加一个私有字段,您可以在其中存储枚举,如下所示:

private buttons button;

并在您的构造函数中,分配枚举:

public MyCounter(long millisInFutre, long countdownInterval, buttons name) {
    super(millisInFutre, countdownInterval);
    this.button = name;
}

那么 onTick 方法应该像往常一样被调用:

public void onTick(long millisUntilFinished) {
    switch(button) {
    case buttons.wraith:
        wraith.setText(...);
        break;
    }
}

希望你能完成这个:)

于 2012-10-18T13:39:40.533 回答