【问题标题】:Countdown Timer required on AndroidAndroid 需要倒数计时器
【发布时间】:2010-08-18 09:00:14
【问题描述】:

这是我讨论过的关于 Java 中格式(mm:ss)的倒数计时器的链接:

Java console code for StopWatch/Timer?

现在我想在 android 的 textview 中显示(和更新)它。有什么线索吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    倒计时

    您将使用 TextField 并使用 CountDownTimer 更新其内容,或查看 Rahul 在同一问题中的回答。

    计数

    在你的活动中

    import android.widget.Chronometer;
    
    ...
    
    private Chronometer crono;
    
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    
     setContentView(R.layout.screen_crono);    
    
     this.crono = (Chronometer) findViewById(R.id.calling_crono);
    
        startCrono();
    }
    
    public void startCrono() {
     crono.setBase(SystemClock.elapsedRealtime());
     crono.start();
    }
    

    阻止它

    crono.stop();
    

    在 XML 布局 screen_crono

    <Chronometer
        android:id="@+id/calling_crono"
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
        android:textStyle="bold"
     android:textSize="14sp"/>
    

    要将其设置在 TextView 中,我认为这是不可能的。把它放在它的右边或左边,这取决于你想要什么。

    如果这不是你想要的,我希望它可以帮助别人。

    【讨论】:

    • 是的,这算数,我的错误,但看起来 Maxood 并不介意 :) 我已经更新了答案。
    【解决方案2】:

    您还可以使用 android 中提供的 CountDownTimer 类。

    只需声明一个构造函数并使用

    启动计时器
    timer test=new timer(30000,1000);
    
    在上述情况下,

    onTick 将每 1000 毫秒触发一次。你可以从这里更新你的 TextView

    class timer extends CountDownTimer
    {
    
     public timer(long millisInFuture, long countDownInterval) 
        {
      super(millisInFuture, countDownInterval);
      // TODO Auto-generated constructor stub
     }
    
     @Override
     public void onFinish() 
        {
    
    
     }
    
     @Override
     public void onTick(long millisUntilFinished) 
        {
      // TODO Auto-generated method stub
                // Update your textview on on tick
    
     }
    
     }
    

    【讨论】:

      【解决方案3】:

      在这篇文章中使用定时更新 UI 的方法真的很好用

      http://developer.android.com/resources/articles/timed-ui-updates.html

      另外,你可以使用内置的 android 时间类来显示你的文本,它会让你的格式化更容易 imo

      http://developer.android.com/reference/android/text/format/Time.html

      【讨论】:

        【解决方案4】:

        试试这个代码.....

        tv = new TextView(this);
            this.setContentView(tv);
        
        //10000 is the starting number (in milliseconds)
        //1000 is the number to count down each time (in milliseconds)
        MyCount counter = new MyCount(10000,1000);
        
        counter.start();
        
        }
        
        //countdowntimer is an abstract class, so extend it and fill in methods
        public class MyCount extends CountDownTimer
        {
        
        public MyCount(long millisInFuture, long countDownInterval) 
        {
        super(millisInFuture, countDownInterval);
        }
        
        public void onFinish()
        {
        tv.setText("Time Up!");
        }
        
        public void onTick(long millisUntilFinished) 
        {
        tv.setText("Time Left : " + millisUntilFinished/1000);
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多