【问题标题】:how to get value from countdowntimer and display into toast如何从倒数计时器中获取价值并显示到吐司中
【发布时间】:2014-10-13 10:42:27
【问题描述】:

如果我点击开始按钮,我有从 1 到 9999 的倒计时计时器,计数将开始,但如果点击停止按钮,我需要从倒计时中获取当前值并在 toast 中显示该值,但如果我点击停止,倒计时无法停止按钮请帮助我

 private CountDownTimer countDownTimer;
         private boolean timerHasStarted = false;
         private Button startB;
         public TextView ;


         private final long startTime = 9999 * 1;

         private final long interval = 1 *1 ;

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

              startB = (Button) this.findViewById(R.id.button);
              startB.setOnClickListener(this);
              text = (TextView) this.findViewById(R.id.timer);
              countDownTimer = new MyCountDownTimer(startTime, interval);
              text.setText(text.getText() + String.valueOf(startTime / 1));
    }

    public void onClick(View v) {
          if (!timerHasStarted) {
           countDownTimer.start();
           timerHasStarted = true;
           startB.setText("STOP");
          } else {
           /*countDownTimer.cancel();
           timerHasStarted = false;
           startB.setText("RESTART");*/
          }
    }

    public class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);

        }

        @Override
        public void onFinish() {

            //text.setText("Time's up!");
            countDownTimer.start();

        }
        @Override
        public void onTick(long millisUntilFinished) {

             text.setText("" + millisUntilFinished / 1);

        }       
    }

谢谢

【问题讨论】:

    标签: android countdowntimer


    【解决方案1】:

    这是我的倒计时:

    QuestionCountdownTimer

    public class QuestionCountdownTimer extends CountDownTimer {
    
    private TextView remainingTimeDisplay;
    private Context context;
    
    public QuestionCountdownTimer(Context context,long millisInFuture, long countDownInterval,TextView remainingTimeDisplay) {
       super(millisInFuture, countDownInterval);
        this.context = context;
       this.remainingTimeDisplay = remainingTimeDisplay;
    }
    
    @Override
    public void onTick(long millisUntilFinished) {
       long millis = millisUntilFinished;
       String hms = String.format("%02d:%02d",
               TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
               TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
       remainingTimeDisplay.setText(hms);
    }
    
    @Override
    public void onFinish() {
       Toast.makeText(context,"COUNTDOWN FINISH :)",Toast.LENGTH_SHORT).show();
    }
    
    }
    

    注意: TextView 剩余时间显示

    remainingTimeDisplay.setText(hms); 我用它来使用 TextView 显示剩余时间

    我在这里调用计时器:

    //Start Quiz timer
    QuestionCountdownTimer timer = new QuestionCountdownTimer(this,10000, 1000, remainingTimeDisplay);
        timer.start();
    

    -第一个参数:this - 我用它作为上下文来显示 Toast 消息

    -second参数:10000-总时间(10秒)

    -第三个参数:1000-倒计时间隔(1秒)

    -last参数:实时显示剩余时间

    经过测试并且可以正常工作

    【讨论】:

      【解决方案2】:

      像这样创建您的 CountDownTimer:

      public class MyCountDownTimer extends CountDownTimer
          {
              private long timePassed = 0;
      
              public MyCountDownTimer(long startTime, long interval)
              {
                  super(startTime, interval);
              }
      
              @Override
              public void onFinish()
              {
                  //text.setText("Time's up!");
                  countDownTimer.start();
              }
      
              @Override
              public void onTick(long millisUntilFinished)
              {
                  timePassed++;
                  text.setText("" + millisUntilFinished / 1);
              }
      
              public long getTimePassed()
              {
                  return timePassed;
              }
          }
      

      然后在你的 onClick 上做:

      ((MyCoundDownTimer) countDownTimer).getTimePassed();
      

      检索时间并将您的 textview 文本设置为它。

      【讨论】:

      • 我把这行放在哪里 ((MyCoundDownTimer) countDownTimer).getTimePassed();
      • how to get value from countdowntimer and display into toast。该行就是您问题的答案。
      • 我照你说的做了,但输出没有变化
      • 现在输出改变了吗?如果这就是你的要求,吐司永远不会改变它的输出。一旦你打印了一个 toast,它就永远不会改变它的文本。您必须再打印一份。
      • 现在我的问题是我有四个文本视图,如果我单击开始按钮,我声明了 0 到 9 个值,如果我单击停止按钮,倒计时将从 0 到 9 在每个文本视图中开始,我将获得随机值像 4001,8963 假设如果我只需要像 400,896 这样的三位数字,我该怎么做
      【解决方案3】:

      你应该使用处理程序

      private Handler tickResponseHandler = new Handler() {
          public void handleMessage(Message msg) {
              int time = msg.what;
              //make toast or do what you want
          }
      }
      

      并将其传递给 MyCountDownTimer 构造函数

      private Handler handler;
      
      public MyCountDownTimer(long startTime, long interval, Handler handler) {
              super(startTime, interval);
              this.handler = handler;
      }
      

      并发送消息

      @Override
      public void onTick(long millisUntilFinished) {
          text.setText("" + millisUntilFinished / 1);
          Message msg = new Message();
          msg.what = millisUntilFinished/1;
          handler.sendMessage(msg);
      }     
      

      这就是你需要做的:)

      【讨论】:

        猜你喜欢
        • 2020-08-02
        • 1970-01-01
        • 2011-07-10
        • 2020-04-08
        • 2014-09-18
        • 1970-01-01
        • 2014-05-13
        • 2019-10-17
        • 1970-01-01
        相关资源
        最近更新 更多