永发信息网

android中怎么实现倒计时怎么按照小时分钟

答案:2  悬赏:80  手机版
解决时间 2021-11-30 11:46
android中怎么实现倒计时怎么按照小时分钟
最佳答案
Android实现计时与倒计时的五种方法:

方法一
Handler与Message(不用TimerTask)

public class timerTask extends Activity{
private int recLen = 11;
private TextView txtView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);

Message message = handler.obtainMessage(1); // Message
handler.sendMessageDelayed(message, 1000);
}

final Handler handler = new Handler(){

public void handleMessage(Message msg){ // handle message
switch (msg.what) {
case 1:
recLen--;
txtView.setText("" + recLen);

if(recLen > 0){
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 1000); // send message
}else{
txtView.setVisibility(View.GONE);
}
}

super.handleMessage(msg);
}
};
}
linux

方法二
Handler与Thread(不占用UI线程)

public class timerTask extends Activity{
private int recLen = 0;
private TextView txtView;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);

new Thread(new MyThread()).start(); // start thread
}

final Handler handler = new Handler(){ // handle
public void handleMessage(Message msg){
switch (msg.what) {
case 1:
recLen++;
txtView.setText("" + recLen);
}
super.handleMessage(msg);
}
};

public class MyThread implements Runnable{ // thread
@Override
public void run(){
while(true){
try{
Thread.sleep(1000); // sleep 1000ms
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}catch (Exception e) {
}
}
}
}
}

方法三
Timer与TimerTask(Java实现)

public class timerTask extends Activity{

private int recLen = 11;
private TextView txtView;
Timer timer = new Timer();

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);

timer.schedule(task, 1000, 1000); // timeTask
}

TimerTask task = new TimerTask() {
@Override
public void run() {

runOnUiThread(new Runnable() { // UI thread
@Override
public void run() {
recLen--;
txtView.setText(""+recLen);
if(recLen < 0){
timer.cancel();
txtView.setVisibility(View.GONE);
}
}
});
}
};
}
方法四
TimerTask与Handler(不用Timer的改进型)

public class timerTask extends Activity{
private int recLen = 11;
private TextView txtView;
Timer timer = new Timer();

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);

timer.schedule(task, 1000, 1000); // timeTask
}

final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what) {
case 1:
txtView.setText(""+recLen);
if(recLen < 0){
timer.cancel();
txtView.setVisibility(View.GONE);
}
}
}
};

TimerTask task = new TimerTask() {
@Override
public void run() {
recLen--;
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
}
方法五
Handler与Runnable(最简单型)

public class timerTask extends Activity{
private int recLen = 0;
private TextView txtView;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);

handler.postDelayed(runnable, 1000);
}

Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
recLen++;
txtView.setText("" + recLen);
handler.postDelayed(this, 1000);
}
};
}
计时与倒计时

方法1,方法2和方法3,都是倒计时

方法4,方法5,都是计时

计时和倒计时,都可使用上述方法实现(代码稍加改动)

UI线程比较

方法1,方法2和方法3,都是在UI线程实现的计时;

方法4和方法5,是另开Runnable线程实现计时

实现方式比较

方法1,采用的是Java实现,即Timer和TimerTask方式;

其它四种方法,都采用了Handler消息处理

推荐使用

如果对UI线程交互要求不很高,可以选择方法2和方法3

如果考虑到UI线程阻塞,严重影响到用户体验,推荐使用方法4,另起线程单独用于计时和其它的逻辑处理

方法5,综合了前几种方法的优点,是最简的
全部回答
Android实现计时与倒计时的五种方法:

方法一
Handler与Message(不用TimerTask)

public class timerTask extends Activity{
private int recLen = 11;
private TextView txtView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);

Message message = handler.obtainMessage(1); // Message
handler.sendMessageDelayed(message, 1000);
}

final Handler handler = new Handler(){

public void handleMessage(Message msg){ // handle message
switch (msg.what) {
case 1:
recLen--;
txtView.setText("" + recLen);

if(recLen > 0){
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 1000); // send message
}else{
txtView.setVisibility(View.GONE);
}
}

super.handleMessage(msg);
}
};
}
linux

方法二
Handler与Thread(不占用UI线程)

public class timerTask extends Activity{
private int recLen = 0;
private TextView txtView;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);

new Thread(new MyThread()).start(); // start thread
}

final Handler handler = new Handler(){ // handle
public void handleMessage(Message msg){
switch (msg.what) {
case 1:
recLen++;
txtView.setText("" + recLen);
}
super.handleMessage(msg);
}
};

public class MyThread implements Runnable{ // thread
@Override
public void run(){
while(true){
try{
Thread.sleep(1000); // sleep 1000ms
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}catch (Exception e) {
}
}
}
}
}

方法三
Timer与TimerTask(Java实现)

public class timerTask extends Activity{

private int recLen = 11;
private TextView txtView;
Timer timer = new Timer();

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);

timer.schedule(task, 1000, 1000); // timeTask
}

TimerTask task = new TimerTask() {
@Override
public void run() {

runOnUiThread(new Runnable() { // UI thread
@Override
public void run() {
recLen--;
txtView.setText(""+recLen);
if(recLen < 0){
timer.cancel();
txtView.setVisibility(View.GONE);
}
}
});
}
};
}
方法四
TimerTask与Handler(不用Timer的改进型)

public class timerTask extends Activity{
private int recLen = 11;
private TextView txtView;
Timer timer = new Timer();

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);

timer.schedule(task, 1000, 1000); // timeTask
}

final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what) {
case 1:
txtView.setText(""+recLen);
if(recLen < 0){
timer.cancel();
txtView.setVisibility(View.GONE);
}
}
}
};

TimerTask task = new TimerTask() {
@Override
public void run() {
recLen--;
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
}
方法五
Handler与Runnable(最简单型)

public class timerTask extends Activity{
private int recLen = 0;
private TextView txtView;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);

handler.postDelayed(runnable, 1000);
}

Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
recLen++;
txtView.setText("" + recLen);
handler.postDelayed(this, 1000);
}
};
}
计时与倒计时

方法1,方法2和方法3,都是倒计时

方法4,方法5,都是计时

计时和倒计时,都可使用上述方法实现(代码稍加改动)

UI线程比较

方法1,方法2和方法3,都是在UI线程实现的计时;

方法4和方法5,是另开Runnable线程实现计时

实现方式比较

方法1,采用的是Java实现,即Timer和TimerTask方式;

其它四种方法,都采用了Handler消息处理

推荐使用

如果对UI线程交互要求不很高,可以选择方法2和方法3

如果考虑到UI线程阻塞,严重影响到用户体验,推荐使用方法4,另起线程单独用于计时和其它的逻辑处理

方法5,综合了前几种方法的优点,是最简的
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
个子矮,脚大女生正常么
珠州至重庆北火在时刻表
米房上一共借到手三万三还了三万六七了,现在
大家昨天都怎么过的啊,有表白的吗?
有哪些表现世事变迁的诗词
年三十晚出月光,下句
镇雄农村下石脚方法
如果你的老板说其他同事坏话,或者老板问你那
为什么现在DHL到付,还要公司出示保函
请问竖吹双管巴乌怎么选调
求清静天下第一系列小说
公务员职务与职级并行还需哪些配套政策
骑行问题,近视眼要带眼镜,那有怎样带骑自行
集众思,广忠益.宽小过,总大纲;是什么意思
我想请问哈,我是1991年7月10号下午4点我是阴
推荐资讯
长安金牛星到底好不好?
佛山勒流到佛山禅荷花岛有多少公里
匡玺霖多少画
佛说解冤经,众生多接冤
成语什么生什么
单选题以下有关秦朝封建专制主义制度的说法不
我想买一台二手手机,哪个平台好?找靓机怎么
如何识别骗子金融公司
上海322路夜班公交取消了,等了快一个小时都
脱式算 3.5-2.75+0.86
当我闺蜜接过我送给她礼物的时候.作文500字
与其他动、植物等生命相比,人的生命独特性突
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?