欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现时间倒计时功能

程序员文章站 2023-12-02 17:40:40
本文实例为大家分享了android实现时间倒计时功能展示的具体代码,供大家参考,具体内容如下 效果展示 mainactivity(主页面代码) pub...

本文实例为大家分享了android实现时间倒计时功能展示的具体代码,供大家参考,具体内容如下

效果展示

Android实现时间倒计时功能

mainactivity(主页面代码)

public class mainactivity extends activity {

  private relativelayout countdown;
  // 倒计时
  private textview daystv, hourstv, minutestv, secondstv;
  private long mday = 10;
  private long mhour = 10;
  private long mmin = 30;
  private long msecond = 00;// 天 ,小时,分钟,秒
  private boolean isrun = true;
  private handler timehandler = new handler() {

    @override
    public void handlemessage(message msg) {
      super.handlemessage(msg);
      if (msg.what==1) {
        computetime();
        daystv.settext(mday+"");
        hourstv.settext(mhour+"");
        minutestv.settext(mmin+"");
        secondstv.settext(msecond+"");
        if (mday==0&&mhour==0&&mmin==0&&msecond==0) {
          countdown.setvisibility(view.gone);
        }
      }
    }
  };

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    countdown = (relativelayout) findviewbyid(r.id.countdown_layout);
    daystv = (textview) findviewbyid(r.id.days_tv);
    hourstv = (textview) findviewbyid(r.id.hours_tv);
    minutestv = (textview) findviewbyid(r.id.minutes_tv);
    secondstv = (textview) findviewbyid(r.id.seconds_tv);

    startrun();
  }

  /**
   * 开启倒计时
   */
  private void startrun() {
    new thread(new runnable() {

      @override
      public void run() {
        // todo auto-generated method stub
        while (isrun) {
          try {
            thread.sleep(1000); // sleep 1000ms
            message message = message.obtain();
            message.what = 1;
            timehandler.sendmessage(message);
          } catch (exception e) {
            e.printstacktrace();
          }
        }
      }
    }).start();
  }

  /**
   * 倒计时计算
   */
  private void computetime() {
    msecond--;
    if (msecond < 0) {
      mmin--;
      msecond = 59;
      if (mmin < 0) {
        mmin = 59;
        mhour--;
        if (mhour < 0) {
          // 倒计时结束
          mhour = 23;
          mday--;
        }
      }
    }
  }
}

main(主页面布局)

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/countdown_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@android:color/white"
  android:gravity="center" >

  <relativelayout
    android:id="@+id/daojishi_rl"
    android:layout_width="match_parent"
    android:layout_height="40.0dip"
    android:layout_marginleft="10.0dip"
    android:layout_marginright="10.0dip"
    android:gravity="center" >

    <imageview
      android:id="@+id/describe_iv"
      android:layout_width="40.0dip"
      android:layout_height="40.0dip"
      android:background="@drawable/clock"
      android:gravity="center_vertical" />

    <textview
      android:id="@+id/describe_tv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centervertical="true"
      android:layout_marginright="5.0dip"
      android:layout_torightof="@+id/describe_iv"
      android:text="距离活动还有 "
      android:textsize="25sp" />

    <textview
      android:id="@+id/days_tv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centervertical="true"
      android:layout_torightof="@+id/describe_tv"
      android:textcolor="@color/coloraccent"
      android:gravity="center"
      android:text="20"
      android:textsize="20sp" />

    <textview
      android:id="@+id/colon0"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centervertical="true"
      android:layout_marginleft="5.0dip"
      android:layout_marginright="3.0dip"
      android:layout_torightof="@+id/days_tv"
      android:text="天"
      android:textsize="20sp"
      android:textstyle="bold" />
  </relativelayout>

  <relativelayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/daojishi_rl"
    android:gravity="center_horizontal" >

    <textview
      android:id="@+id/hours_tv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centervertical="true"
      android:layout_toleftof="@+id/colon1"
      android:textcolor="@color/coloraccent"
      android:gravity="center"
      android:text="23"
      android:textsize="20sp" />

    <textview
      android:id="@+id/colon1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centervertical="true"
      android:layout_marginleft="3.0dip"
      android:layout_marginright="3.0dip"
      android:layout_toleftof="@+id/minutes_tv"
      android:text=":"
      android:textsize="20sp"
      android:textstyle="bold" />

    <textview
      android:id="@+id/minutes_tv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centervertical="true"
      android:layout_toleftof="@+id/colon2"
      android:textcolor="@color/coloraccent"
      android:gravity="center"
      android:text="59"
      android:textsize="20sp" />

    <textview
      android:id="@+id/colon2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centervertical="true"
      android:layout_marginleft="3.0dip"
      android:layout_marginright="3.0dip"
      android:layout_toleftof="@+id/seconds_tv"
      android:text=":"
      android:textsize="20sp"
      android:textstyle="bold" />

    <textview
      android:id="@+id/seconds_tv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignparentright="true"
      android:layout_centervertical="true"
      android:gravity="center"
      android:text="59"
      android:textcolor="@color/coloraccent"
      android:textsize="20sp" />
  </relativelayout>

</relativelayout>

谢谢观看,小编祝大家生活愉快!

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