布局:

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. Silverlight技术调查(2)——跨域访问

    原文 Silverlight技术调查(2)——跨域访问 此调查web容器采用的是Tomcat,若允许所有域访问,只需在webapps下的根应用ROOT中,加入配置文件:clientaccesspoli ...

  2. Windows Azure 安全最佳实践 - 第 2 部分:Azure 提供哪些现成可用的安全机制

    在WindowsAzure安全最佳实践 - 部分:深度解析挑战防御对策中,我介绍了威胁形势以及在您的应用程序中采用深度防御的计划. 在本部分中,我将说明 Windows Azure的安全是一项共同责任 ...

  3. Qt Quick鼠标事件处理、键盘、计时器

    在<Qt Quick 事件处理之信号与槽>中介绍了 QML 中怎样使用内建类型的信号以及怎样自己定义信号,这次我们来看看怎样处理鼠标.键盘.定时器等事件.这些时间在处理时,一般是通过信号来 ...

  4. Android 环境变量配置(Mac)

    Mac 系统10.10,自带的就是jdk1.6,因为工作需要就升级到了1.7,要从新配置环境变量了 mac 默认是自带的有jdk1.6 安装路径为: /System/Library/Framework ...

  5. 博弈论之Nim

    博弈论(一):Nim游戏 重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P-position当且仅当a1^a2^...^an=0,其中^表示位异或(xor)运算. Nim游戏是博 ...

  6. [置顶] oracle 数据库表中转换成java代码

    --数据库中字段java代码 select col.TABLE_NAME,replace(initcap(col.TABLE_NAME),'_', '')   , 'private '||decode ...

  7. ListView+CheckBox两种解决方式及原因分析

    近期在用ListView+CheckBox搞一个item选中的项目,我将CheckBox的focus设置为false,另我大喜的是,CheckBox居然能够选中(窃喜中),这么简单就搞定了,由于数据量 ...

  8. 3xian退役贴【深思。】

    这是原文: 最后一天,漫天飘起了雪花,假装欢送我离去. 这次WF之战不太顺利,早期的C题大概花了1秒钟构思,然而由于输出格式多了一个空格直到两个半小时才逃脱Wrong Answer的纠缠.还好lynn ...

  9. 手把手教你修改pcduino系统默认的音频输出

    最近要搞个小玩意儿,要用到pcduino的音频输出,但是系统默认的是输出到hdmi的音频,我的显示器上没有喇叭,只能搞个USB声卡.但是系统默认又不是输出到USB,这里我手把手叫你怎么设置系统默认声卡 ...

  10. web.xml中listener作用及使用

    一.WebContextLoaderListener 监听类 它能捕捉到server的启动和停止,在启动和停止触发里面的方法做对应的操作! 它必须在web.xml 中配置才干使用,是配置监听类的 二. ...