2016-08-31 11:25:28 qq_33897656 阅读数 678
  • 微信小程序开发实战第三季

    本季完成一个辩论赛计时器APP,涉及到动画使用、声音播放、action-sheet、tabBar、表单元素form、switch、slider、radio-group、button等,涉及到面向对象的编程思路

    18640 人正在学习 去看看 邹积超

倒计时代码如下:


package com.myresume.cn.activity;


import com.myresume.cn.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;

import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView tvCount;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        tvCount=(TextView) findViewById(R.id.tvCount);
        new TimeCount(4000, 1000).start();//计时开始

    }

    //定义一个倒计时的内部类
    class TimeCount extends CountDownTimer{
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
            
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            tvCount.setText(millisUntilFinished/1000+"");
            
        }

        @Override
        public void onFinish() {//计时完毕时触发该方法
            Intent intent=new Intent(MainActivity.this, HomepageActivity.class);
            startActivity(intent);
            finish();//跳转activity后,关闭掉当前的额activity
            
        }
        
    }

}



          有什么问题可以给我留言。

          我的微博http://weibo.com/laokuzai  安卓派,关注android最新资讯

          微信:zhuj8989,加微信畅聊android技术


2012-05-29 10:01:52 yizhouuser 阅读数 43
  • 微信小程序开发实战第三季

    本季完成一个辩论赛计时器APP,涉及到动画使用、声音播放、action-sheet、tabBar、表单元素form、switch、slider、radio-group、button等,涉及到面向对象的编程思路

    18640 人正在学习 去看看 邹积超
php 团购倒计时 代码


<?php

//$a = strtotime(date("Y-m-d H:i:s"));

$a = strtotime(date("2012-5-28 17:50:51"));//自定义时间
$b = $a+24*3600;//24小时后的时间戳的值
$d = $a-24*3600;//24小时前的时间戳的值
echo date("Y-m-d H:i:s",$b);//24小时后的时间
echo "</br>";
echo date("Y-m-d H:i:s",$d);//24小时前的时间
echo "</br>";
$c=mktime();//当前时间戳值
echo date("Y-m-d H:i:s");//当前时间
echo "</br>";
echo $c;//当前时间戳值
echo "</br>";
echo(strtotime("+5 hours")); //5小时后的时间戳值
echo "</br>";
$f=$b-$c;
echo strtotime(date("Y-m-d H:i:s",$f));//离24小时后的剩余的时间戳。(秒)
echo "</br>";

///$days;
//$hours;
//$minute;
//$second;



$seconds=$f;
//$seconds=3722;

$second=$seconds%60;

$minute=(int)($seconds/60);
//$minute=var_dump((int) ($seconds/60));测试代码
//echo $minute;
//echo (int)$chushu;

$minutes=$minute%60;
$hours=(int)($minute/60);

if($seconds>60){
if($minute>60){
echo '剩余时间:  ';
echo $hours.'小时'.$minutes.'分'.$second.'秒';

}
else{
echo $minutes.'分'.$second.'秒';
}
}else{
if($f<=0){echo '你可以投票了';}
else{
echo $seconds.'秒';
}
}



?>
2019-03-18 15:31:00 w517272812 阅读数 85
  • 微信小程序开发实战第三季

    本季完成一个辩论赛计时器APP,涉及到动画使用、声音播放、action-sheet、tabBar、表单元素form、switch、slider、radio-group、button等,涉及到面向对象的编程思路

    18640 人正在学习 去看看 邹积超

@ Android ListView中有倒计时
如果ListView中有倒计时,那么倒计时一定要自定义View,如果使用notifyDataChangge()更新页面会很卡。
下面我贴出我项目中的倒计时代码

Java代码

public class CountDownLL extends LinearLayout {
private TextView hourTv, minTv, secondTv;
private Context context;
private CountDownTimer countDownTimer;
private Drawable itemBg;
public CountDownLL(Context context) {
    this(context, null, 0);
}

public CountDownLL(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
}

public CountDownLL(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.count_down);
    itemBg=typedArray.getDrawable(R.styleable.count_down_item_bg);
    typedArray.recycle();
    initView();
}

public void initView() {
    View view = LayoutInflater.from(context).inflate(R.layout.coundown, null);
    addView(view);
    hourTv = view.findViewById(R.id.hour_tv);
    if (itemBg!=null){
        hourTv.setBackground(itemBg);
    }
    minTv = view.findViewById(R.id.minute_tv);
    minTv.setBackground(itemBg);
    secondTv = view.findViewById(R.id.second_tv);
    secondTv.setBackground(itemBg);
}


public void setCountDownNum(long countDownNum) {
    if (countDownNum <= 0) {
        return;
    }
    countDownTimer = new CountDownTimer(countDownNum * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            CountDownLL.this.setVisibility(VISIBLE);
            TimeModel timeModel = longToTimeModel(millisUntilFinished / 1000);
            hourTv.setText(timeModel.getHour());
            minTv.setText(timeModel.getMinute());
            secondTv.setText(timeModel.getSecond());
        }

        @Override
        public void onFinish() {
            stopTimer();
            CountDownLL.this.setVisibility(GONE);
        }
    };
    countDownTimer.start();
}

public void stopTimer() {
    if (countDownTimer != null) {
        countDownTimer.cancel();
        countDownTimer = null;
    }
}
class TimeModel {
    String day;
    String hour;
    String minute;
    String second;
    TimeModel(String day, String hour, String minute, String second) {
        this.day = day;
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }
    String getDay() {
        return day;
    }
    String getHour() {
        return hour;
    }

    String getMinute() {
        return minute;
    }
    String getSecond() {
        return second;
    }
}

private TimeModel longToTimeModel(long m) {
    long min = 60;
    long hour = min * 60;
    long day = 24 * hour;

    long dayNum = m / day;
    long hourNum = m % day / hour;
    long minNum = m % hour / min;
    long secNum = m % min;
    String hourNumStr;
    if (0 <= hourNum && hourNum <= 9) {
        hourNumStr = "0" + hourNum;
    } else {
        hourNumStr = "" + hourNum;
    }
    String minNumStr;
    if (0 <= minNum && minNum <= 9) {
        minNumStr = "0" + minNum;
    } else {
        minNumStr = "" + minNum;
    }
    String secNumStr;
    if (0 <= secNum && secNum <= 9) {
        secNumStr = "0" + secNum;
    } else {
        secNumStr = "" + secNum;
    }
    return new TimeModel(dayNum + "", hourNumStr, minNumStr, secNumStr);
}

布局文件

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/day_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0天"
    android:textColor="#333333"
    android:textSize="13dp"
    android:visibility="gone" />

<TextView
    android:id="@+id/hour_tv"
    android:layout_width="17dp"
    android:layout_height="14dp"
    android:background="@drawable/time_bg"
    android:gravity="center"
    android:text="00"
    android:textColor="@color/white"
    android:textSize="11dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=":"
    android:textColor="#333333"
    android:textSize="11dp" />

<TextView
    android:id="@+id/minute_tv"
    android:layout_width="17dp"
    android:layout_height="14dp"
    android:background="@drawable/time_bg"
    android:gravity="center"
    android:text="00"
    android:textColor="@color/white"
    android:textSize="11dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=":"
    android:textColor="#333333"
    android:textSize="11dp" />

<TextView
    android:id="@+id/second_tv"
    android:layout_width="17dp"
    android:layout_height="14dp"
    android:background="@drawable/time_bg"
    android:gravity="center"
    android:text="00"
    android:textColor="@color/white"
    android:textSize="11dp" />
# 在attrs文件中添加
<declare-styleable name="count_down">
    <attr name="item_bg" format="reference"></attr>
</declare-styleable>
2016-06-05 21:50:55 yanlixu 阅读数 596
  • 微信小程序开发实战第三季

    本季完成一个辩论赛计时器APP,涉及到动画使用、声音播放、action-sheet、tabBar、表单元素form、switch、slider、radio-group、button等,涉及到面向对象的编程思路

    18640 人正在学习 去看看 邹积超


//主页面
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.exddample.time.MainActivity" >


    <TextView
        android:id="@+id/tv_time1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


    <TextView
        android:id="@+id/tv_time2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_time1"
        android:text="@string/hello_world" />


    <TextView
        android:id="@+id/tv_time3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_time2"
        android:text="@string/hello_world" />


    <Button
        android:id="@+id/btn_jump"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="点击跳转倒计时" />


</RelativeLayout>








///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//倒计时页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


    <TextView
        android:id="@+id/tv_timeshow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="倒计时"
        android:textSize="30sp" />


</LinearLayout>








////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//主页面




package com.exddample.time;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


@SuppressLint({ "SimpleDateFormat", "UseValueOf" })
public class MainActivity extends Activity {


private TextView time1;
private TextView time2;
private TextView time3;
private Button btn_jump;
private long diff;
private int days;
private int hours;
private int minutes;
private int seconds;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


time1 = (TextView) findViewById(R.id.tv_time1);
time2 = (TextView) findViewById(R.id.tv_time2);
time3 = (TextView) findViewById(R.id.tv_time3);
btn_jump = (Button) findViewById(R.id.btn_jump);


// // String time="1463126553000";
// 获取当前时间


Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1 = formatter.format(currentTime);
time1.setText(date1);
// 获取服务器返回的时间戳 转换成"yyyy-MM-dd HH:mm:ss"
String date2 = formatData("yyyy-MM-dd HH:mm:ss", 1463126553);
time2.setText(date2);


// 计算的时间差
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


try {
Date d1 = df.parse(date1);
Date d2 = df.parse(date2);
diff = d1.getTime() - d2.getTime();


days = (int) (diff / (1000 * 60 * 60 * 24));
hours = (int) ((diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
minutes = (int) ((diff - days * (1000 * 60 * 60 * 24) - hours
* (1000 * 60 * 60))
/ (1000 * 60));
seconds = (int) ((diff - days * (1000 * 60 * 60 * 24) - hours
* (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000);


time3.setText("" + days + "天" + hours + "小时" + minutes + "分"
+ seconds + "秒");
} catch (Exception e) {
e.printStackTrace();
}


btn_jump.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("hours", hours);
intent.putExtra("minutes", minutes);
intent.putExtra("seconds", seconds);
intent.setClass(getApplicationContext(), TimeActivity.class);
startActivity(intent);
}
});


}


public static String formatData(String dataFormat, long timeStamp) {
if (timeStamp == 0) {
return "";
}
timeStamp = timeStamp * 1000;
String result = "";
SimpleDateFormat format = new SimpleDateFormat(dataFormat);
result = format.format(new Date(timeStamp));
return result;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}










////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//倒计时页面


package com.exddample.time;


import java.util.Timer;
import java.util.TimerTask;


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.widget.TextView;


@SuppressLint("HandlerLeak")
public class TimeActivity extends Activity {


private TextView tv_timeshow;
private TimerTask timerTask;
Timer timer;
final static String tag = "tag";
private int hour;
private int minute;
private int second;


public TimeActivity() {
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);


Intent intent = getIntent();


hour = intent.getIntExtra("hours", -1);
minute = intent.getIntExtra("minutes", -1);
second = intent.getIntExtra("seconds", -1);


tv_timeshow = (TextView) findViewById(R.id.tv_timeshow);


tv_timeshow.setText(hour + ":" + minute + ":" + second);


timerTask = new TimerTask() {

@Override
public void run() {
Message msg = new Message();
msg.what = 0;
handler.sendMessage(msg);
}
};
timer = new Timer();
timer.schedule(timerTask, 0, 1000);
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
System.out.println("handle!");
if (hour == 0) {
if (minute == 0) {
if (second == 0) {// 时间到
tv_timeshow.setText("Time out !");
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
} else {// 00:30,00:08
second--;
if (second >= 10) {
tv_timeshow.setText("0" + hour + ":" + "0" + minute
+ ":" + second);
} else {
tv_timeshow.setText("0" + hour + ":" + "0" + minute
+ ":0" + second);
}
}
} else {// minute!=0,33:00->32:59
if (second == 0) {
second = 59;
minute--;
if (minute >= 10) {// 33:30
tv_timeshow.setText("0" + hour + ":" + minute + ":"
+ second);
} else {// 06:30
tv_timeshow.setText("0" + hour + ":" + "0" + minute
+ ":" + second);
}
} else {// 32:30->32:29
second--;
if (second >= 10) {
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":" + second);
} else {// 09:30
tv_timeshow.setText("0" + hour + ":" + "0"
+ minute + ":" + second);
}
} else {// 12:30
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":0" + second);
} else {// 08:30
tv_timeshow.setText("0" + hour + ":" + "0"
+ minute + ":0" + second);
}
}
}
}
}


// hour>0
else {
if (minute == 0) {// hour>0,minute=0
if (second == 0) {
minute = 59;
second = 59;
hour--;
if (hour >= 10) {
if (second >= 10){
tv_timeshow.setText(hour + ":" + minute + ":" + second);
}else{
tv_timeshow.setText(hour + ":" + minute + ":0" + second);
}
} else {
if (second >= 10){
tv_timeshow.setText("0"+hour + ":" + minute + ":" + second);
}else{
tv_timeshow.setText("0"+hour + ":" + minute + ":0" + second);
}
}
} else {
second--;
if (hour >= 10) {
if (second >= 10){
tv_timeshow.setText(hour + ":0" + minute + ":" + second);
}else{
tv_timeshow.setText(hour + ":0" + minute + ":0" + second);
}
} else {
if (second >= 10){
tv_timeshow.setText("0"+hour + ":0" + minute + ":" + second);
}else{
tv_timeshow.setText("0"+hour + ":0" + minute + ":0" + second);
}
}
}
} else {// hour>0,minute!=0
if (second == 0) {
second = 59;
minute--;
if (hour >= 10) {
if (second >= 10) {
if (minute >= 10) {
tv_timeshow.setText(hour + ":" + minute + ":"
+ second);
} else {
tv_timeshow.setText(hour + ":0" + minute + ":"
+ second);
}
} else {
if (minute >= 10) {
tv_timeshow.setText(hour + ":" + minute + ":0"
+ second);
} else {
tv_timeshow.setText(hour + ":0" + minute
+ ":0" + second);
}
}
} else {
if (second >= 10) {
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":" + second);
} else {
tv_timeshow.setText("0" + hour + ":0" + minute
+ ":" + second);
}
} else {
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":0" + second);
} else {
tv_timeshow.setText("0" + hour + ":0" + minute
+ ":0" + second);
}
}
}


} else {
second--;
if (hour >= 10) {
if (second >= 10) {
if (minute >= 10) {
tv_timeshow.setText(hour + ":" + minute + ":"
+ second);
} else {
tv_timeshow.setText(hour + ":0" + minute + ":"
+ second);
}
} else {
if (minute >= 10) {
tv_timeshow.setText(hour + ":" + minute + ":0"
+ second);
} else {
tv_timeshow.setText(hour + ":0" + minute
+ ":0" + second);
}
}
} else {
if (second >= 10) {
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":" + second);
} else {
tv_timeshow.setText("0" + hour + ":0" + minute
+ ":" + second);
}
} else {
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":0" + second);
} else {
tv_timeshow.setText("0" + hour + ":0" + minute
+ ":0" + second);
}
}
}


}
}
}
};
};
}
2018-05-11 10:13:17 huanhuan59 阅读数 1412
  • 微信小程序开发实战第三季

    本季完成一个辩论赛计时器APP,涉及到动画使用、声音播放、action-sheet、tabBar、表单元素form、switch、slider、radio-group、button等,涉及到面向对象的编程思路

    18640 人正在学习 去看看 邹积超

1.首先要写一个倒计时类-Android笔记之倒计时CountDownTimer抽象类

CountDownTimer类的使用很简单,这是一个android.os包下面的抽象类,其构造方法需要传入两个值,第一个参数是你需要倒计时的总时间,单位为毫秒,第二个值为计时变化依据的间隔时间,单位为毫秒.以下是CountDownTimer的构造方法


    //构造方法
    public CountDownTimer(long millisInFuture, long countDownInterval) {
        mMillisInFuture = millisInFuture;
        mCountdownInterval = countDownInterval;
    }

有两个方法必须要重写,而且两个方法都是在UI线程中执行的,不用担心子线程无法更新UI的问题:
onTick(long millisUntilFinished)

每隔设定的间隔时间就会执行此方法,millisUntilFinished为剩余的毫秒值,根据你设置的两个参数值决定的


onFinish()

倒计时结束执行的回调方法

2.代码:倒计时类CountDownTimerUtils.java

package com.example.myapp.Baseactivity;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.widget.Button;

import com.example.myapp.R;

public class CountDownTimerUtils extends CountDownTimer {
    private Button mButton;

    public CountDownTimerUtils(Button button,long millisInFuture, long countDownInterval) {//控件,定时总时间,间隔时间
        super(millisInFuture, countDownInterval);
        this.mButton=button;

    }

    @Override
    public void onTick(long millisUntilFinished) {
        mButton.setClickable(false);//设置不可点击
        mButton.setText(millisUntilFinished/1000+"秒后可重新发送");//设置倒计时时间
        /* SpannableString spannableString=new SpannableString(bt_getcord.getText().toString());//获取按钮上的文字
        ForegroundColorSpan span=new ForegroundColorSpan(Color.RED);//设置文字颜色
        bt_getcord.setAllCaps(false);
        spannableString.setSpan(span,0,2,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);////将倒计时的时间设置为红色
        bt_getcord.setText(spannableString);*/

    }

    @Override
    public void onFinish() {
        mButton.setClickable(true);//重新获得点击
        mButton.setText("重新获取验证码");

    }

}

在登陆界面调用这个类就可以倒计时了,填写三句话

第一句:定义类与变量


第二句:创建对象


第三句:点击获取验证码btn_getcord时,就开始倒计时了

最后的结果:


超级简单JS网页倒计时代码

博文 来自: cngdjws

Android倒计时实现

阅读数 809

没有更多推荐了,返回首页