Countdown Timer required on Android

前端 未结 4 1735
粉色の甜心
粉色の甜心 2020-12-06 23:05

Here is the link where i have discussed about a countdown timer of format(mm:ss) in Java:

Java console code for StopWatch/Timer?

Now i want to display (and u

相关标签:
4条回答
  • 2020-12-06 23:13

    To count down

    You'll use a TextField and update its content using CountDownTimer, or check Rahul's answer in this same question.

    To count up

    In your Activity

    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();
    }
    

    To stop it

    crono.stop();
    

    In the XML Layout screen_crono

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

    To set it inside a TextView, I don't think that's possible. Place it to its right or to its left, depending on what you want.

    If this is not what you wanted, I hope it helps someone else.

    0 讨论(0)
  • 2020-12-06 23:20

    try this code.....

    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);
    
    }
    
    0 讨论(0)
  • 2020-12-06 23:23

    You can also use the CountDownTimer class available in android.

    Just declare a constructor and start the timer with

    timer test=new timer(30000,1000);
    

    onTick will get fired once in every 1000ms in the above case. You can update your TextView from here

    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
    
     }
    
     }
    
    0 讨论(0)
  • 2020-12-06 23:36

    Use the method of a timed UI update in this article it really works great

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

    Also, you might use the built in android time class to display your text, it will make your formatting much easier imo

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

    0 讨论(0)
提交回复
热议问题