このテキストビューを設定したい:
<TextView
Android:id="@+id/timer"
Android:layout_width="100dp"
Android:layout_height="30dp"
Android:layout_above="@+id/directions"/>
タイマーになること。したがって、テキストは30秒間0:30から0:29 ...のようなタイマーになるはずです。タイマーが0:00になると、別のメソッドを呼び出すことができます。 「再試行」を印刷してタイマーを再起動できます。
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);
}
コードを試してください:
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);
}
}
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();
}
}
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();
}
_ 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
@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);
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);
}