如何显示在Android的计时器 [英] How to display the timer in android
本文介绍了如何显示在Android的计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要显示在TextView的计时器在像[19:59]。所以当我点击开始按钮,计时器会显示这样的例子,我想设置高达20 mintues的格式,它会显示像[19:58] [19:87]。可谁能给一些想法或例如code?
解决方案
包com.example.testproject;
进口java.util.Timer中;
进口java.util.TimerTask中;
进口android.os.Bundle;
进口android.widget.TextView;
进口android.app.Activity;
公共类MainActivity延伸活动{
公众诠释秒= 60;
公众诠释分钟= 10;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
//声明定时器
计时器t =新的Timer();
//设置日程表功能和速率
t.scheduleAtFixedRate(新的TimerTask(){
@覆盖
公共无效的run(){
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
TextView的电视=(TextView中)findViewById(R.id.main_timer_text);
tv.setText(将String.valueOf(分钟)+:+将String.valueOf(秒));
秒 - = 1;
如果(秒== 0)
{
tv.setText(将String.valueOf(分钟)+:+将String.valueOf(秒));
秒= 60;
分钟=分钟; 1
}
}
});
}
},0,1000);
}
}
I want to display the timer in TextView in the format of like [ 19:59].so when i click the start button ,the timer will display like this for example,i want to set upto 20 mintues,it will display like [19:58][19:87].can anyone give some ideas or example code?
解决方案
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);
}
}
这篇关于如何显示在Android的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文