布局:

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中倒计时代码的更多相关文章

  1. android中倒计时控件CountDownTimer分析

    android中倒计时控件CountDownTimer分析 1 示例代码 new CountDownTimer(10000, 1000) { public void onTick(long milli ...

  2. Android中通过代码获取arrays.xml文件中的数据

    android工程res/valuse文件夹下的arrays.xml文件中用于放各种数组数据,比如字符串数组.整型数组等,数组中的数据可能是具体的值,也有可能是对资源数据的引用,下面针对这两种情况通过 ...

  3. android中通过代码来设置蓝牙永久可见性

    废话不多说,直接阐述: 前段时间在搞一个android项目,其中有一个功能要求需要蓝牙可见性永久打开,但是开发过android蓝牙的程序员应该都知道,goole提供的api中没有设置蓝牙永久可见性的接 ...

  4. android中在代码中设置margin属性

    1,不多说,小知识点,直接上代码 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15);// 创 ...

  5. android 中通过代码创建控件

    package bvb.de.openadbwireless.circle; import android.annotation.TargetApi; import android.app.Activ ...

  6. Android 中常用代码片段

    一:AsyncTask 的使用 (1)activity_main.xml <TextView android:id="@+id/tvInfo" android:layout_ ...

  7. Android中webview和js之间的交互(转)

    http://www.cnblogs.com/leizhenzi/archive/2011/06/29/2093636.html 1.android中利用webview调用网页上的js代码. Andr ...

  8. 利用HTML5开发Android(5)---HTML5地理位置服务在Android中的应用

    Android中 Java代码 //启用地理定位 webSettings.setGeolocationEnabled(true); //设置定位的数据库路径 webSettings.setGeoloc ...

  9. android 中webview调用js

    1.android中利用webview调用网页上的js代码. Android 中可以通过webview来实现和js的交互,在程序中调用js代码,只需要将webview控件的支持js的属性设置为true ...

随机推荐

  1. 【FAQ】SpingMVC实现集合參数(Could not instantiate bean class [java.util.List])

    需求,要求批量新增或者改动一个List,在Spring MVC中是不支持以下代码的写法 @RequestMapping(value = "/update", method = Re ...

  2. Java 23种设计模式详尽分析与实例解析之三--行为型模式

    Java设计模式 行为型模式 职责链模式 模式动机:职责链可以是一条直线.一个环或者一个树形结构.链上的每一个对象都是请求处理者,职责链模式可以将请求的处理组织成一条链,并使请求链传递,由链上的处理者 ...

  3. jQuery中对 input 控件的操作

    jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关 1.获取值 jquery取radio单选按钮的值 $(" ...

  4. Android来电监听

    本文实现来电自动接听,自动录音,自动回拨功能. public class MainActivity extends Activity { @Override protected void onCrea ...

  5. SQL_查找用户的表属于哪个表空间

    ***********************************************声明*************************************************** ...

  6. centos 安装ganglia监控工具

    一个.ganglia基本介绍 ganglia它是一个分布式监控系统,那里有两个Daemon,每间:clientGangliaMonitoring Daemon (gmond)和服务端GangliaMe ...

  7. 内核编程实例,多文件的Makefile

    内核编程实例,多文件的Makefile 经典的hello word测试 ////# cat hello.c #include <linux/module.h> #include <l ...

  8. 精讚部落::MySQL 的MEMORY engine

    精讚部落::MySQL 的MEMORY engine MySQL 的MEMORY engine 無次要群組

  9. CF 17B Hierarchy

    Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinat ...

  10. 状态压缩动态规划 -- 棋盘问题 POJ 1321

    一个 N * N 的棋盘上面,有些格子不能放,放置 M 的棋子, 每两个棋子不能在同一行或者同一列,问有多少种放法 DFS太慢,用SCR好点点 Python 仅仅有 22 行,事实上能够更短.可是得排 ...