timer的使用--倒计时--计时器
2010-01-12 20:22
337 查看
倒计时:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/CountDownTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
</LinearLayout>
-------------------------------------------------------------------------------------------------------------------------------------------
countdowntime.java:
package demo.countdowntime;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class countdowntime extends Activity {
Timer timer;
int recLen=50;
TextView recTime;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
coutdowntime();
}
private void coutdowntime() {
recTime=(TextView)findViewById(R.id.CountDownTime);
timer = new Timer(true);
timer.schedule(task,1000, 1000); //延时1000ms后执行,1000ms执行一次
}
TimerTask task = new TimerTask(){
public void run() {
Message message = new Message();
message.what = 1;
for(;recLen>0;recLen--){
handler.sendMessage(message);
}
}
};
final Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
recTime.setText(String.valueOf(recLen));
break;
}
super.handleMessage(msg);
}
};
}
================================================================================
计时器:(原文参见:/article/5665542.html)
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:text="Time"
android:id="@+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
------------------------------------------------------------------------------------------------------------------------
timer.java:
package demo.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class timer extends Activity {
Timer timer;
int recLen;
TextView recTime;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timer();
}
private void timer() {
recTime = (TextView)findViewById(R.id.Time);
timer = new Timer(true);
timer.schedule(task,1000, 1000); //延时1000ms后执行,1000ms执行一次
}
TimerTask task = new TimerTask(){
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
recLen++;
recTime.setText(String.valueOf(recLen));
break;
}
super.handleMessage(msg);
}
};
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/CountDownTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
</LinearLayout>
-------------------------------------------------------------------------------------------------------------------------------------------
countdowntime.java:
package demo.countdowntime;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class countdowntime extends Activity {
Timer timer;
int recLen=50;
TextView recTime;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
coutdowntime();
}
private void coutdowntime() {
recTime=(TextView)findViewById(R.id.CountDownTime);
timer = new Timer(true);
timer.schedule(task,1000, 1000); //延时1000ms后执行,1000ms执行一次
}
TimerTask task = new TimerTask(){
public void run() {
Message message = new Message();
message.what = 1;
for(;recLen>0;recLen--){
handler.sendMessage(message);
}
}
};
final Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
recTime.setText(String.valueOf(recLen));
break;
}
super.handleMessage(msg);
}
};
}
================================================================================
计时器:(原文参见:/article/5665542.html)
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:text="Time"
android:id="@+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
------------------------------------------------------------------------------------------------------------------------
timer.java:
package demo.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class timer extends Activity {
Timer timer;
int recLen;
TextView recTime;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timer();
}
private void timer() {
recTime = (TextView)findViewById(R.id.Time);
timer = new Timer(true);
timer.schedule(task,1000, 1000); //延时1000ms后执行,1000ms执行一次
}
TimerTask task = new TimerTask(){
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
recLen++;
recTime.setText(String.valueOf(recLen));
break;
}
super.handleMessage(msg);
}
};
}
相关文章推荐
- Java计时器Timer的使用!...
- 使用计时器实现倒计时(setTimeout())
- Delphi中用MessageBox()API函数做倒计时对话框(使用Hook安装CBTHookCallback,计时器更改文字,SetWindowText API真正修改文字,引用未知函数)good
- 可等待计时器内核对象的使用(CreateWaitableTimer)
- java计时器Timer的几种使用方法
- 在 ASP.NET 中使用计时器(Timer)转
- Boost中计时器(timer)的使用
- 60秒倒计时登录验证按钮计时器Timer Task
- Silverlight2.0中的计时器类DispatcherTimer——不再使用Storyboard计时器
- timer 计时器的使用
- C#中计时器Timer的三种使用方法
- ASP.NET中使用计时器(Timer)(转)
- QML中计时器的使用,实现一个简单的倒计时程序
- 在 ASP.NET 中使用计时器(Timer)的整理(2003/10/23 20:36)
- [转]Java计时器Timer 使用
- 在 ASP.NET 中使用计时器(Timer)
- Android中三种计时器Timer、CountDownTimer、handler.postDelayed的使用
- 在 ASP.NET 中使用计时器(Timer)
- 在 ASP.NET 中使用计时器(Timer)
- Android 一直不断动态刷新ui使用计时器TImer