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 ...
随机推荐
- MEAN栈开发
Nodejs之MEAN栈开发(二)----视图与模型 2016-06-02 08:30 by stoneniqiu, 92 阅读, 2 评论, 收藏, 编辑 上一节做了对Express做了简单的介绍, ...
- Swift - 使用相机拍摄照片
1,打开相机拍照 通过设置图片控制器UIImagePickerController的来源为UIImagePickerControllerSourceType.Camera,便可以打开相机 1 2 3 ...
- Entity - 使用EF框架进行增删改查 - 模型先行
模型先行:先创建数据库实体模型,然后再进行数据库的增删改查. 基本步骤是不变的,可参照 <Entity - 使用EF框架进行增删改查 - 数据库先行> 其中的不同是,在创建数据库实体模型的 ...
- MFC自绘(17篇)
http://www.cnblogs.com/lidabo/category/477377.html
- Java 的垃圾回收机制(转)
先看一段转载,原文出自 http://jefferent.iteye.com/blog/1123677 虚拟机中的共划分为三个代:年轻代(Young Generation).年老点(Old Gener ...
- 14.2.1 MySQL and the ACID Model
14.2 InnoDB Concepts and Architecture InnoDB 概念和结构体系: 14.2.1 MySQL and the ACID Model 14.2.2 InnoDB ...
- 临界段CCriticalSection的使用
类CCriticalSection的对象表示一个“临界区”,它是一个用于同步的对象,同一时刻仅仅同意一个线程存取资源或代码区.临界区在控制一次仅仅有一个线程改动数据或其他的控制资源时很实用.比如,在链 ...
- XML学习经验实例总结2
DTD约束 Book.dtd: <!ELEMENT 书架 (书+)> <!ELEMENT 书 (书名,价格,介绍)> <!ELEMENT 书名 (#PCDATA)> ...
- openCV中cvSnakeImage()函数代码分析
/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...
- mfc menu用法一
#define IDM_NEW 0x1002#define IDM_EXIT 0x1003#define IDM_HELP 0x1004#define IDM_ABOUT 0x1005 hMenu = ...