使用编辑文本在吐司中简单倒计时

时间:2013-12-27 18:17:33

标签: java android xml

我正在尝试从文本编辑中进行简单的倒计时。然后我创建了编辑文本和其中包含的恢复值,然后我做了一段时间。遵循代码。

while(counter<second) {
    Toast.makeText(getBaseContext(), "Countdown: "+counter++, Toast.LENGTH_LONG).show();
    counter++;

    if(counter==second) {
        break;
    }
}

变量second是int

String seconds = editText.getText().toString();
second = Integer.valueOf(seconds);

当我执行代码故障时显示错误的数字。错误在哪里?

2 个答案:

答案 0 :(得分:1)

尝试更改

while(counter<second) {
                    Toast.makeText(getBaseContext(), "Countdown: "+counter++, Toast.LENGTH_LONG).show();
                    counter++;

                    if(counter==second) {
                        break;
                    }

while(counter<second) {
                    Toast.makeText(getBaseContext(), "Countdown: "+counter, Toast.LENGTH_LONG).show();
                    counter++;

                    if(counter==second) {
                        break;
                    }

你每个循环增加两次计数器

答案 1 :(得分:1)

实现倒计时的更好方法::在edittext中输入值后,通过单击按钮来更改下面的代码来启动计时器:

 int a = Integer.parseInt(editText.getText()); //total time for the timer in millis

    new CountDownTimer(a, 1000) {

         public void onTick(long millisUntilFinished) {
             Toast.makeText(context,"seconds remaining: " + millisUntilFinished / 1000,Toast.LENGTH_SHORT).show();
         }

         public void onFinish() {
             Toast.makeText(context,"done!",Toast.LENGTH_SHORT).show();
         }
      }.start();