您的位置:首页 > 其它

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);

}

};

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: