Android 倒计时类
来源:互联网 发布:公交大数据分析 编辑:程序博客网 时间:2024/05/02 08:51
前言:
在考试APP和网络培训上有很多的应用,自己写的CounterTimer类.
测试用小例子:
package com.elvis.cnttimer;import android.os.Bundle;import android.os.CountDownTimer;import android.app.Activity;import android.graphics.Color;import android.view.Menu;import android.view.Window;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { private TextView txtView; private MyCnt mCnt; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtView = (TextView)findViewById(R.id.textView1); mCnt = new MyCnt(2*1000*3600,1000); mCnt.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public class MyCnt extends CountDownTimer{ public MyCnt(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); // TODO Auto-generated constructor stub } @Override public void onFinish() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "时间到了...", Toast.LENGTH_SHORT); } @Override public void onTick(long m) { // TODO Auto-generated method stub // m 是微秒为单位的 //小时 设置 int myHour = (int)((m/1000)/3600); String myHourStr = myHour + ""; if(myHourStr.length() < 2) { myHourStr = "0" + myHourStr; } //分钟设置 int myMin = (int)(((m/1000)- myHour *3600)/60); String myMinStr = myMin + ""; if(myMinStr.length() < 2) { myMinStr = "0" + myMinStr; } //秒设置 int mySec= (int)((m/1000)- myHour *3600 - myMin * 60); String mySecStr = mySec + ""; if(mySecStr.length() < 2) { mySecStr = "0" + mySecStr; } txtView.setTextSize(22); txtView.setTextColor(Color.RED); txtView.setText("倒计时 \n\n" + myHourStr + "/" + myMinStr + "/" + mySecStr); } }}
注意:
super.onCreate(savedInstanceState);
去掉这句话程序不能运行.因为父类的onCreate()完成了很多我们不能接触的事情来确保我们的app能顺利运行.
0 0
- android倒计时类CountDownTimer
- Android CountDownTimer 倒计时类
- Android 倒计时工具类
- Android 倒计时类
- Android-倒计时工具类
- android 倒计时管理类
- Android 倒计时工具类
- Android内置倒计时类CountDownTimer
- android工具类---------(倒计时)
- Android CountDownTimer 倒计时工具类
- 学习笔记-----Android倒计时类
- 分享一个Android倒计时类
- Android 倒计时
- android倒计时
- 【Android】倒计时
- android 倒计时
- android 倒计时
- Android 倒计时
- 辽宁省赛2010 G题 Friends Number
- Spark Streaming 理解一
- 为图纸中的工作特征增加注释
- JQuery 绑定select标签的onchange事件
- 黑马程序员——c语言函数
- Android 倒计时类
- Python2学习笔记之实现ping和which源码
- 使用sysbench进行cpu/IO/内存/OLTP等基准测试
- sqlsever存储过程练习题
- UVA11880 Ball in a Rectangle
- 类设计和继承的注意事项
- json复杂数据与Java对象的转换
- IOC之bean之间的关系讲解
- MySQL字符集乱码总结