Android实现倒计时结束后跳转页面功能

Marmara ·
更新时间:2024-09-21
· 559 次阅读

前言

在开发中会经常用到倒计时这个功能,关于倒计时的实现,有疑问的朋友们可以参考这篇://www.jb51.net/article/101807.htm

本文主要给大家介绍了关于Android倒计时结束跳转页面的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

示例代码

1.layout中新建两个xml文件,在src下的包中新建两个类,MainActivity和MainActivity2并分别指向两个xml文件,在MainActivity的指向的xml文件建一个TextView控件,用于倒计时的显示。

2.MainActivity文件中的代码如下

package com.example.demo1; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.textView1); handler.post(waitSendsRunnable); } //启用一个Handler Handler handler = new Handler() { @SuppressLint("HandlerLeak") public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: Intent intent = new Intent(MainActivity.this, MainActivity2.class); startActivity(intent); break; case 1: tv1.setText("倒计时:" + index + "s"); break; default: break; } } }; // 倒计时五秒 int index = 5; Runnable waitSendsRunnable = new Runnable() { public void run() { if (index > 0) { index--; try { Thread.sleep(1000); handler.sendEmptyMessage(1); } catch (InterruptedException e) { e.printStackTrace(); } handler.post(waitSendsRunnable); } else { try { Thread.sleep(1000); handler.sendEmptyMessage(0); } catch (InterruptedException e) { e.printStackTrace(); } } } }; }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对软件开发网的支持。

您可能感兴趣的文章:android自定义倒计时控件示例android实现倒计时功能代码Android实现计时与倒计时的常用方法小结Android自定义圆形倒计时进度条Android实现倒计时方法汇总Android实现倒计时30分钟功能Android启动页面定时跳转的三种方法Android利用CountDownTimer实现倒计时功能 Android实现停留5s跳转到登录页面Android 实现页面跳转Android使用Intent实现页面跳转



跳转页面 倒计时 Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号