0

使用 android studio 我有一个带有CountDownTimer. 当我按下一个按钮时,它从 10 秒开始到 0 秒,并且TextView计算我在一个按钮上点击了多少次,到目前为止一切都很好。我想通过按另一个按钮重新启动来重新启动此活动。你能帮助我吗?如果有帮助,我把代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtCount = (TextView)findViewById(R.id.textView1);
    txtCount.setText(String.valueOf(count));
    btnCount = (Button)findViewById(R.id.button1);
    btnRestart = (Button)findViewById(button2);

    final boolean[] timerProcessing = {false};
    final boolean[] timerStarts = {false};

    final TextView textViewTimer = (TextView)findViewById(R.id.textView2);
    //Saving link to timer object
    final CountDownTimer timer = new CountDownTimer(10000, 1) {

        public void onTick(long millisUntilFinished) {
            textViewTimer.setText("" + millisUntilFinished / 1000
                    + ":" + millisUntilFinished % 1000);
                        }


        public void onFinish() {
            textViewTimer.setText("0:000");
            timerProcessing[0] = false;
        }

    };

    btnCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            //start timer once when button first click
            if (!timerStarts[0]){
                timer.start();
                timerStarts[0] = true;
                timerProcessing[0] = true;
            }

            if (timerProcessing[0]){
                count++;
                txtCount.setText(String.valueOf(count));
            }
        }
    });
    btnRestart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {  }
}
4

3 回答 3

2

只需替换以下代码:

private TextView txtCount, textViewTimer;
private Button btnCount, btnRestart;
int count = 0;
boolean[] timerProcessing = { false };
boolean[] timerStarts = { false };
private MyCount timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtCount = (TextView) findViewById(R.id.textView1);
    txtCount.setText(String.valueOf(count));
    btnCount = (Button) findViewById(R.id.button1);
    btnRestart = (Button) findViewById(R.id.button2);

    textViewTimer = (TextView) findViewById(R.id.textView2);

    timer = new MyCount(10000, 1);

    btnCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            // start timer once when button first click
            if (!timerStarts[0]) {
                timer.start();
                timerStarts[0] = true;
                timerProcessing[0] = true;
            }

            if (timerProcessing[0]) {
                count++;
                txtCount.setText(String.valueOf(count));
            }
        }
    });
    btnRestart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            timerProcessing[0] = true;
            count = 0;
            txtCount.setText(String.valueOf(count));
            timer.cancel();
            timer.start();
        }
    });
}

public class MyCount extends CountDownTimer {
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        textViewTimer.setText("0:000");
        timerProcessing[0] = false;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        textViewTimer.setText("" + millisUntilFinished / 1000 + ":"
                + millisUntilFinished % 1000);

    }
}

在这里,您的计数器变量被替换为内部类,因此您无需每次都创建计数器变量。如果要重新启动计数器,只需创建一次计数器变量并调用它的 start 方法。

于 2013-10-17T08:07:37.677 回答
1

将方法包装CountDownTimer在一个方法中,然后在单击重新启动按钮时再次调用它。

于 2013-10-17T02:32:56.930 回答
0

为什么一定要重启Activity。您需要有一个逻辑并处理单击侦听器上的重启按钮。

将您的倒数计时器代码移动到一个函数,并从您的活动 onCreate/onResume 以及单击重新启动按钮调用此函数。

于 2013-10-16T23:30:40.707 回答