web-dev-qa-db-fra.com

Comment afficher une minuterie dans un TextView sous Android?

Je veux définir cette textview:

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

Être un chronométreur. Le texte devrait donc être une minuterie allant de 0:30 à 0:29 ... pendant 30 secondes. Une fois que la minuterie est à minuit, je peux appeler une autre méthode. Je peux imprimer "essayer à nouveau" et redémarrer le chronomètre.

4
Larry Jing
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);
}
11
user7348593

Essayez le code:

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

}
3
W4R10CK

Fichier 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>

Activité  

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
Yogesh Borhade
@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);
0
Steve Hame