Android中倒计时代码
布局:
maina.xml
<DigitalClock
android:id="@+id/myClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:textSize="30sp" /> <TextView
android:id="@+id/text_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/myClock"
android:layout_centerHorizontal="true"
android:text="@string/text_select"
android:textSize="20sp" /> <EditText android:id="@+id/minute"
android:layout_width="60dp"
android:layout_height="80dp"
android:layout_below="@id/text_select"
android:layout_alignLeft="@id/myClock"
android:layout_marginTop="20dp"
android:inputType="number"
android:gravity="center"
/> <EditText android:id="@+id/second"
android:layout_width="60dp"
android:layout_height="80dp"
android:layout_below="@id/text_select"
android:layout_toRightOf="@id/minute"
android:layout_marginTop="20dp"
android:inputType="number"
android:gravity="center"
/> <Button
android:id="@+id/button_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:padding="10dp"
android:text="@string/myButtonText"
android:textSize="30sp" />
start.xml
<TextView android:id="@+id/myTime"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="30dp"
android:textSize="100sp"
android:textColor="#FF0000"
android:gravity="center"
android:textStyle="bold" />
MainActivity.java
public class MainActivity extends Activity {
Button startButton;
EditText minuteText;
EditText secondText;
int minute;
int second;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
startButton = (Button) findViewById(R.id.button_start);
minuteText = (EditText)findViewById(R.id.minute);
secondText = (EditText)findViewById(R.id.second);
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!minuteText.getText().toString().equals("")) {
minute = Integer.parseInt(minuteText.getText().toString());
}
if (!secondText.getText().toString().equals("")) {
second = Integer.parseInt(secondText.getText().toString());
}
if (minute != 0 || second != 0) {
System.out.println(minute+":"+second);
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(minute);
list.add(second);
Intent intent = new Intent();
intent.setAction("com.example.mytime.StartActivity");
intent.putIntegerArrayListExtra("times", list);
startActivity(intent);
}
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
minute = 0;
second = 0;
super.onResume();
}
}
StartActivity.java
public class StartActivity extends Activity{
static int minute = -1;
static int second = -1;
final static String tag = "tag";
TextView timeView;
Timer timer;
TimerTask timerTask;
Handler handler = new Handler(){
public void handleMessage(Message msg) {
System.out.println("handle!");
if (minute == 0) {
if (second == 0) {
timeView.setText("Time out !");
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
}else {
second--;
if (second >= 10) {
timeView.setText("0"+minute + ":" + second);
}else {
timeView.setText("0"+minute + ":0" + second);
}
}
}else {
if (second == 0) {
second =59;
minute--;
if (minute >= 10) {
timeView.setText(minute + ":" + second);
}else {
timeView.setText("0"+minute + ":" + second);
}
}else {
second--;
if (second >= 10) {
if (minute >= 10) {
timeView.setText(minute + ":" + second);
}else {
timeView.setText("0"+minute + ":" + second);
}
}else {
if (minute >= 10) {
timeView.setText(minute + ":0" + second);
}else {
timeView.setText("0"+minute + ":0" + second);
}
}
}
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v(tag, "log---------->onCreate!");
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
timeView = (TextView)findViewById(R.id.myTime);
if (minute == -1 && second == -1) {
Intent intent = getIntent();
ArrayList<Integer> times = intent.getIntegerArrayListExtra("times");
minute = times.get(0);
second = times.get(1);
}
timeView.setText(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);
}
@Override
protected void onDestroy() {
Log.v(tag, "log---------->onDestroy!");
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
minute = -1;
second = -1;
super.onDestroy();
}
@Override
protected void onStart() {
Log.v(tag, "log---------->onStart!");
super.onStart();
}
@Override
protected void onStop() {
Log.v(tag, "log---------->onStop!");
super.onStop();
}
@Override
protected void onResume() {
Log.v(tag, "log---------->onResume!");
super.onResume();
}
@Override
protected void onRestart() {
Log.v(tag, "log---------->onRestart!");
super.onRestart();
}
@Override
protected void onPause() {
Log.v(tag, "log---------->onPause!");
super.onPause();
}
}
本文本详细源码文件请访问:http://download.csdn.net/detail/chrp99/5646983
更多源码请访问:http://download.csdn.net/user/chrp99/uploads
Android中倒计时代码的更多相关文章
- android中倒计时控件CountDownTimer分析
android中倒计时控件CountDownTimer分析 1 示例代码 new CountDownTimer(10000, 1000) { public void onTick(long milli ...
- Android中通过代码获取arrays.xml文件中的数据
android工程res/valuse文件夹下的arrays.xml文件中用于放各种数组数据,比如字符串数组.整型数组等,数组中的数据可能是具体的值,也有可能是对资源数据的引用,下面针对这两种情况通过 ...
- android中通过代码来设置蓝牙永久可见性
废话不多说,直接阐述: 前段时间在搞一个android项目,其中有一个功能要求需要蓝牙可见性永久打开,但是开发过android蓝牙的程序员应该都知道,goole提供的api中没有设置蓝牙永久可见性的接 ...
- android中在代码中设置margin属性
1,不多说,小知识点,直接上代码 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15);// 创 ...
- android 中通过代码创建控件
package bvb.de.openadbwireless.circle; import android.annotation.TargetApi; import android.app.Activ ...
- Android 中常用代码片段
一:AsyncTask 的使用 (1)activity_main.xml <TextView android:id="@+id/tvInfo" android:layout_ ...
- Android中webview和js之间的交互(转)
http://www.cnblogs.com/leizhenzi/archive/2011/06/29/2093636.html 1.android中利用webview调用网页上的js代码. Andr ...
- 利用HTML5开发Android(5)---HTML5地理位置服务在Android中的应用
Android中 Java代码 //启用地理定位 webSettings.setGeolocationEnabled(true); //设置定位的数据库路径 webSettings.setGeoloc ...
- android 中webview调用js
1.android中利用webview调用网页上的js代码. Android 中可以通过webview来实现和js的交互,在程序中调用js代码,只需要将webview控件的支持js的属性设置为true ...
随机推荐
- [Boost基础]并发编程——asio网络库——异步socket处理
异步服务器端 #include <conio.h> #include <iostream> using namespace std; #include <boost/as ...
- HDU 2544 最短
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2544 解析: 首先数据量为V<=100 那么这里使用不论什么基础的最短路的算法都不会超时! 常见数 ...
- µC/OS-II版本升级指南
IDE: MDK V4+ MCU: LPC17xx(Cortex-M3) RTOS: µC/OS-II 升级顺序:V2.52->V2.62->V2.76-> ...
- Qt图片显示效率的比较 转
转http://blog.sina.com.cn/s/blog_5c70dfc80100r257.html 在Qt中处理图片一般都要用到QImage类,但是QImage的对象不能够直接显示出来,要想能 ...
- 单击Android设备后退键,主屏幕键以及旋转屏幕如何影响Activity的生命周期
单击设备的后退键,相当于通知Android系统“我已完成activity的使用,现在不需要它了.”接到指令后,系统立即销毁了activity.即调用onPause()->onStop()-> ...
- perl 继承概述
<pre name="code" class="html">[root@wx03 test]# cat Horse.pm package Horse ...
- Round Numbers
转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1301472836 大致题意: 输入两个十进制正整数a和b,求闭区间 [a ,b] 内有多少 ...
- zoj1028-Flip and Shift
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=28 题意:有相互交叉的黑白两种颜色的小球,每一个小球每次可以跳两格:问你是否可以 ...
- GDI 总结三: CImage类使用
前言 CImage类是基于GDI+的.可是这里为什么要讲归于GDI? 主要是基于这种考虑: 在GDI+环境中,我们能够直接使用GDI+ ,没多少必要再使用CImage类 可是,假设再 ...
- oracle至mysql该指南的数据模式()任意数据源之间的跨导应用
为了产生的一些资源的库的释放.需要API模块迁移到mysql在,需要引导数据. 试用oracle to mysql工具.当迁移错误不说,如此大量的数据的,有了这样简陋的工具是不太可靠. 意外的发现工具 ...