如何在Android中的TextView中显示计时器?

时间:2017-01-06 05:17:45

标签: java android xml timer

我想设置此textview:

<TextView
    android:id="@+id/timer"
    android:layout_width="100dp"
    android:layout_height="30dp"
    android:layout_above="@+id/directions"/>

成为计时器。因此,文本应该是一个计时器,如0:30然后0:29 ...持续30秒。一旦计时器是0:00,我可以调用另一种方法;我可以打印出来&#34;再试一次&#34;并重新启动计时器。

7 个答案:

答案 0 :(得分:14)

int time=30;
TextView textTimer = (TextView)findViewById(R.id.timer);

new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {  
        textTimer.setText("0:"+checkDigit(time));
        time--;         
    }

    public void onFinish() {
        textTimer.setText("try again");
    }

}.start();



public String checkDigit(int number) {
    return number <= 9 ? "0" + number : String.valueOf(number);
}

答案 1 :(得分:3)

尝试代码:

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

    public int seconds = 60;
    public int minutes = 10;

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

        //Declare the timer
        Timer t = new Timer();
        //Set the schedule function and rate
        t.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        TextView tv = (TextView) findViewById(R.id.main_timer_text);
                        tv.setText(String.valueOf(minutes)+":"+String.valueOf(seconds));
                        seconds -= 1;

                        if(seconds == 0)
                        {
                            tv.setText(String.valueOf(minutes)+":"+String.valueOf(seconds));

                            seconds=60;
                            minutes=minutes-1;

                        }



                    }

                });
            }

        }, 0, 1000);
    }

}

答案 2 :(得分:3)

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/mtextTimer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start" />
</LinearLayout>

活动

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView textTimer;

    private Button StartTimer;


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

        //Initializing the views
        textTimer = (TextView) findViewById(R.id.mtextTimer);

        StartTimer = (Button) findViewById(R.id.buttonSend);

        StartTimer.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {


        long maxTimeInMilliseconds = 30000;// in your case

        startTimer(maxTimeInMilliseconds, 1000);

    }

    public void startTimer(final long finish, long tick) {
        CountDownTimer t;
        t = new CountDownTimer(finish, tick) {

            public void onTick(long millisUntilFinished) {
                long remainedSecs = millisUntilFinished / 1000;
                textTimer.setText("" + (remainedSecs / 60) + ":" + (remainedSecs % 60));// manage it accordign to you
            }

            public void onFinish() {
                textTimer.setText("00:00:00");
                Toast.makeText(MainActivity.this, "Finish", Toast.LENGTH_SHORT).show();

                cancel();
            }
        }.start();
    }
}

答案 3 :(得分:1)

public void settimer() {
    new CountDownTimer(30000, 1000) {
        public void onTick(long millisUntilFinished) {
            long remainedSecs = millisUntilFinished / 1000;
            forgotpasswordBinding.txtotptime.setText("" + (remainedSecs / 60) + ":" + (remainedSecs % 60));
        }
        public void onFinish() {
            forgotpasswordBinding.txtotptime.setText("Resend");
            forgotpasswordBinding.txtotptime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
            settimer();
                }
            });
        }
    }.start();
}

答案 4 :(得分:0)

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

    Timer t = new Timer();    //declare the timer
    t.scheduleAtFixedRate(new TimerTask() { //Set the schedule function and rate

        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    TextView mRefreshTime = (TextView) findViewById(R.id.screenStatusTvTime);
                    mRefreshTime.setText(String.format("%dh%02d'%02d\"", minutes/60, minutes%60, seconds));
                    if (++seconds == 60) {
                        seconds = 0;
                        minutes++;
                    }
                }
            });
        }
    }, 0L, 1000L);

答案 5 :(得分:0)

 long MillisecondTime, TimeBuff, UpdateTime = 0L;
 int Seconds, Minutes, MilliSeconds;
 TextView textViewTimer = findViewById(R.id.timerTextView);

 public void startTimer() {
    new CountDownTimer(180000, 1000) {

        public void onTick(long millisUntilFinished) {

            MillisecondTime = millisUntilFinished;

            UpdateTime = TimeBuff + MillisecondTime;

            Seconds = (int) (UpdateTime / 1000);

            Minutes = Seconds / 60;

            Seconds = Seconds % 60;

            MilliSeconds = (int) (UpdateTime % 1000);

            textViewTimer.setText("0" + Minutes + ":"
                    + String.format("%02d", Seconds) + ":"
                    + String.format("",MilliSeconds));
        }

        public void onFinish() {
             textViewTimer.setText("Done!");

        }
    }.start();


}

调用startTimer()方法,这会将timer设置为textView,例如开始时间02:59:00

答案 6 :(得分:0)

timer will set for 3min like "2:59"
    private int time=60,n=2; 

        textTimer=findViewById(R.id.timer);
        // textview 
                    new CountDownTimer(180000, 1000) {
                        public void onTick(long millisUntilFinished)
                        {
                            if(time==0)
                            {
                                n--;
                                time=60;
                            }

                            textTimer.setText( n +":" + checkdigit(time));
                            time--;
                        }
                        public void onFinish() {

                        }
                    }.start();
     public String checkdigit(int number)
        {
            return number <= 9 ? "0" + number : String.valueOf(number);
        }