0

私のアプリにはいくつかのボタンがあり、それらのテキストはカウンターです。異なるカウンターを持つ各ボタン。2 独立したい方から始めたい。私の問題は、1 つのボタンを押すと、他のすべてのボタン カウンターが開始することです。そのため、1つだけ押されたときにカウンターを開始するためのボタンがたくさんあります。これには多くのlolアプリがあることは知っていますが、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

1 つのオプションは、MyCounter クラスで列挙型を使用することです。

コンストラクターで単純に割り当ててから、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 に答える