我想在 TextView 中以类似 [19:59] 的格式显示计时器。所以当我单击开始按钮时,计时器将显示为例如,我想设置最多 20 分钟,它将显示为 [19 :58][19:87]。任何人都可以给出一些想法或示例代码吗?
问问题
33666 次
7 回答
25
您可以使用CountDownTimer
.
http://developer.android.com/reference/android/os/CountDownTimer.html
TextView _tv = (TextView) findViewById( R.id.textView1 );
new CountDownTimer(20*60000, 1000) {
public void onTick(long millisUntilFinished) {
_tv.setText("seconds remaining: " +new SimpleDateFormat("mm:ss:SS").format(new Date( millisUntilFinished)));
}
public void onFinish() {
_tv.setText("done!");
}
}.start();
要取消,只需在计时器上调用取消。
public final void cancel()
取消倒计时。
于 2013-07-05T04:39:42.103 回答
17
package com.example.testproject;
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);
}
}
于 2013-07-05T10:14:54.987 回答
6
//start button click
CountDown timer = new CountDown(180000, 1000);
timer.start();
//stop button click
timer.stop();
//countdown class
public class CountDown extends CountDownTimer {
public CountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long ms = millisUntilFinished;
String text = String.format("%02d\' %02d\"",
TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
textViewTimer.setText(text);
}
@Override
public void onFinish() {
textViewTimer.setText("ffinish");
}
}
于 2015-01-14T11:26:44.253 回答
6
这样做activity_timer.xml
<android.support.v7.widget.LinearLayoutCompat
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Chronometer
android:id="@+id/chronometer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.LinearLayoutCompat>
在活动中
public class TimerActivity extends AppCompatActivity {
private Chronometer chronometer2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
chronometer2 = findViewById(R.id.chronometer2);
chronometer2.start();
}
}
您也可以使用停止
chronometer2.stop();
重新开始
chronometer2.setBase(SystemClock.elapsedRealtime());
chronometer2.start();
于 2018-03-30T07:33:37.550 回答
1
在这里,我使用 Timer 类来显示计时器
public class FourthActivity extends AppCompatActivity {
Button startButton, pauseButton;
TextView timerValue;
Timer timer;
int seconds = 0, minutes = 0, hour = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fourth);
bindView();
timer = new Timer();
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (seconds == 60) {
timerValue.setText(String.format("%02d", hour) + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds));
minutes = seconds / 60;
seconds = seconds % 60;
hour = minutes / 60;
}
seconds += 1;
timerValue.setText(String.format("%02d", hour) + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds));
}
});
}
}, 0, 1000);
}
});
}
private void bindView() {
timerValue = (TextView) findViewById(R.id.timerValue);
startButton = (Button) findViewById(R.id.startButton);
}
}
在布局中,有一个 TextView 和一个 Button 来启动计时器。我有使用 Timer 类方法scheduleAtFixedRate
。时间将以 hh:mm:ss 形式显示。
于 2017-07-03T13:43:24.637 回答
0
我为 textView 中的计时器显示编写了代码。格式用于例如:-02:59:00
您可以点击此链接获取Step By Step
代码。
于 2019-10-10T11:31:30.253 回答
0
这可以使用cronometer来完成,最主要原因是支持API 1+,这比支持API 17+的TextClock令人印象深刻。
你可以用 cronometer 做很多其他的事情。例如,您可以在其中设置开始时间
chronometer.setBase(SystemClock.elapsedRealtime());
您还可以在此处了解更多信息。
于 2017-06-24T18:41:47.227 回答