Android仿活动时分秒倒计时效果

 更新时间:2018年02月09日 09:42:57   作者:还有星星  
这篇文章主要为大家详细介绍了Android仿活动时分秒倒计时效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android时分秒倒计时效果的具体代码,供大家参考,具体内容如下

从mian.xml下手:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center"
  android:orientation="vertical" >

  <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="离结束时间为:"/>

  <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:orientation="horizontal">

  <TextView
    android:id="@+id/tvtime1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:background="#3B3B3B"
    android:text="12"
    android:textColor="#FFFFFF"
    android:textSize="30sp" />

  <TextView
    android:id="@+id/tvtime2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:gravity="center"
    android:text="12"
    android:textColor="#FFFFFF"
    android:background="#3B3B3B"
    android:textSize="30sp" />

  <TextView
    android:id="@+id/tvtime3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
     android:layout_marginLeft="10dp"
    android:gravity="center"
    android:background="#3B3B3B"
    android:textColor="#FFFFFF"
    android:text="13"
    android:textSize="30sp" />

  </LinearLayout>

</LinearLayout>

MainActivity

public class MainActivity extends Activity {
 private TextView tvtime1,tvtime2,tvtime3;
 private long time=400;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  tvtime1=(TextView)findViewById(R.id.tvtime1);
  tvtime2=(TextView) findViewById(R.id.tvtime2);
  tvtime3=(TextView) findViewById(R.id.tvtime3);
  handler.postDelayed(runnable, 1000); 
 }


 Handler handler = new Handler(); 
 Runnable runnable = new Runnable() { 
  @Override 
  public void run() { 
   time--; 
   String formatLongToTimeStr = formatLongToTimeStr(time);
   String[] split = formatLongToTimeStr.split(":");
   for (int i = 0; i < split.length; i++) {
    if(i==0){
     tvtime1.setText(split[0]+"小时");
    }
    if(i==1){
     tvtime2.setText(split[1]+"分钟");
    }
    if(i==2){
     tvtime3.setText(split[2]+"秒");
    }

   }
   if(time>0){
    handler.postDelayed(this, 1000); 
   }
  } 
 }; 

 public String formatLongToTimeStr(Long l) {  
   int hour = 0;  
   int minute = 0;  
   int second = 0;  
   second = l.intValue() ;  
   if (second > 60) {   
   minute = second / 60;   //取整 
   second = second % 60;   //取余
   }  

   if (minute > 60) {   
   hour = minute / 60;   
   minute = minute % 60;  
   }  
   String strtime = hour+":"+minute+":"+second;
   return strtime; 

 }

}

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android实现双曲线折线图

    Android实现双曲线折线图

    这篇文章主要为大家详细介绍了Android实现双曲线折线图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • Android实现图片轮播列表

    Android实现图片轮播列表

    这篇文章主要为大家详细介绍了Android实现图片轮播列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Android一行代码实现圆形头像

    Android一行代码实现圆形头像

    本篇文章主要介绍了Android一行代码实现圆形头像的相关知识,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-05-05
  • Android开发自学笔记(三):APP布局上

    Android开发自学笔记(三):APP布局上

    这篇文章主要介绍了Android开发自学笔记(三):APP布局上,本文讲解了添加ViewGroup、添加ViewGroup、定义string内容、添加Button、运行程序查看效果等内容,需要的朋友可以参考下
    2015-04-04
  • Flutter配置代理抓包实现过程详解

    Flutter配置代理抓包实现过程详解

    这篇文章主要为大家介绍了Flutter配置代理抓包实现过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Android自定义ViewGroup实现淘宝商品详情页

    Android自定义ViewGroup实现淘宝商品详情页

    这篇文章主要为大家详细介绍了Android自定义ViewGroup实现淘宝商品详情页,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • android自定义view实现圆周运动

    android自定义view实现圆周运动

    这篇文章主要为大家详细介绍了android自定义view实现逆时针和顺时针转动的圆周运动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-03-03
  • Android中Dialog对话框的使用小结

    Android中Dialog对话框的使用小结

    这篇文章主要给大家总结了一些关于Android中Dialog对话框的使用方法,这其中包括普通对话框、确定取消对话框、多按钮对话框、列表对话框、带Adapter的对话框、单选对话框以及多选对话框等,需要的朋友可以参考学习,下面来一起看看详细的介绍吧。
    2017-04-04
  • Android条纹进度条的实现(调整view宽度仿进度条)

    Android条纹进度条的实现(调整view宽度仿进度条)

    这篇文章主要给大家介绍了关于Android实现条纹进度条的方法,主要是通过调整view宽度仿进度条,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起看看吧
    2018-09-09
  • Android实现短信验证功能的代码

    Android实现短信验证功能的代码

    这篇文章主要介绍了Android实现短信验证功能的代码的相关资料,需要的朋友可以参考下
    2016-07-07

最新评论