介绍
本篇文章为大家展示了如何在Android中使用处理器与Countdowntimer实现一个倒计时功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
<强>实现方法强>
去除actionBar
闪屏页面一般都为全屏显示,这里我们首先需要去除actionBar,在res/价值/风格。xml中设置:
这里也建议大家在后期开发中尽量不要用死板的actionBar,可以根据项目需求使用工具栏或者自定义TitleBar组件来替代actionBar,这样的话界面设计会更加灵活。
2.2布局布局
这里仅仅设置布局背景图片,以及在右上角添加TextView用于显示倒计时,做的有点糙,见谅,代码如下:
& lt; ? xml version=?.0“,编码=皍tf-8" ?比; & lt; RelativeLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android" ,xmlns:工具=癶ttp://schemas.android.com/tools" ,android: id=癅 + id/activity_splash" ,android: layout_width=癿atch_parent" ,android: layout_height=癿atch_parent" ,android:背景=癅mipmap/background_login" ,工具:上下文=癱om.mly.panhouye.handlerdemo.SplashActivity"祝辞 & lt; TextView ,android:重力=皉ight" ,android: id=癅 + id/tv_time" 输入textColor=? android: @color/colorAccent" ,android: layout_width=癿atch_parent" ,android: layout_height=皐rap_content" ,android:文本=癝S" ,android: textSize=?0 sp"/比; & lt;/RelativeLayout>
2.3 java实现代码
2.1中只是去除了app的ActionBar,要做的全屏显示,仍需要在活动中使用代码设置。
package com.mly.panhouye.handlerdemo; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer; import handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; public class  SplashActivity extends AppCompatActivity  { ,private MyHandler MyHandler =, new MyHandler (); ,private TextView  tv_time; ,private MyCountDownTimer  mc; ,@Override ,protected void  onCreate (Bundle savedInstanceState), { ,super.onCreate (savedInstanceState); ,//设置活动为全屏 ,getWindow () .setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, ,WindowManager.LayoutParams.FLAG_FULLSCREEN); ,//去标题状态栏 ,requestWindowFeature (Window.FEATURE_NO_TITLE); ,setContentView (R.layout.activity_splash);=,tv_time (TextView), findViewById (R.id.tv_time);=,,mc  new MyCountDownTimer (5000,, 1000); ,mc.start ();/* * ,*使用处理器的postDelayed延迟5秒执行页面跳转 ,*(与CountDownTimer的millisInFuture一致) ,*/,myHandler.postDelayed (new Runnable (), { ,@Override ,public void  run (), { ,startMainActivity (); ,} },5000); ,} ,//将处理程序声明为静态内部类 ,private static  class MyHandler  extends Handler { ,@Override ,public void  handleMessage (Message 味精),{ ,super.handleMessage(味精); ,} ,} ,//页面跳转的方法 ,private void  startMainActivity () { ,Intent Intent =, new 意图(这个,Main3Activity.class); ,startActivity(意图); ,完成();//完成跳转后销毁闪屏页(从栈内移除) ,} ,class MyCountDownTimer  extends CountDownTimer  {/* * ,* @param millisInFuture ,*表示以毫秒为单位,倒计时的总数 ,*,例如,millisInFuture=1000,表示1秒 ,* @param countDownInterval ,*表示,间隔,多少微秒,调用一次,onTick 方法 ,*,例如:countDownInterval =1000,,表示每1000毫秒调用一次onTick () ,*/,public MyCountDownTimer (long millisInFuture, long countDownInterval), { ,超级(millisInFuture, countDownInterval); ,} ,public void  onFinish (), { ,tv_time.setText(“正在跳转“); ,} ,public void  onTick (long millisUntilFinished), { ,tv_time.setText(“倒计时(“时间+大敌;;millisUntilFinished /, 1000, +,“)“); ,Log.i (“tag",“倒计时“+ millisUntilFinished /, 1000); ,} ,} ,@Override ,protected void  onDestroy (), { ,super.onDestroy (); ,//闪屏页销毁时将消息对象从消息队列移除并结束倒计时 ,myHandler.removeCallbacksAndMessages(空); ,mc.cancel (); ,Log.i (“tag",“destory"); ,} }如何在Android中使用处理器与Countdowntimer实现一个倒计时功能