请教各位大神 倒计时怎么实现的(比如这种:如果是今天14:22:59 明天13:00要一活动 那么倒计时显示也来的就是距活动时间还有11时38分1秒,秒钟是时时更新的)希望各位大神帮帮心忙啊  我在这谢谢  最好有个小例子  邮箱 [email protected] 在这先谢谢各位大神了啊

解决方案 »

  1.   

    我写了一个,是同一天的,只要你输入2个时间(hh mm),然后开始倒计时,java代码如下
    import java.util.Timer;
    import java.util.TimerTask;import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;public class CountDownDemoActivity extends Activity implements OnClickListener {
    TextView text;
    Button submit;
    EditText start, end;
    int hour = 0;
    int minute = 0;
    int second = 0;
    Handler handler = new Handler() { @Override
    public void handleMessage(Message message) {
    switch (message.what) {
    case 0:
    text.setText("倒计时:" + hour + "时" + minute + "分" + second + "秒");
    msg.what = 2;
    break;
    case 1:
    new AlertDialog.Builder(CountDownDemoActivity.this).setTitle(
    "计时结束!").setPositiveButton("确定",
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog,
    int which) {
    CountDownDemoActivity.this.finish();
    }
    }).show();
    msg.what = 2;
    break;
    }
    super.handleMessage(message); }
    }; Message msg; @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (TextView) findViewById(R.id.text);
    submit = (Button) findViewById(R.id.btn);
    submit.setOnClickListener(this);
    start = (EditText) findViewById(R.id.start);
    end = (EditText) findViewById(R.id.end);
    } @Override
    public void onClick(View v) {
    String s1[] = start.getText().toString().split(" ");
    String s2[] = end.getText().toString().split(" ");
    int n1[] = new int[s1.length];
    int n2[] = new int[s2.length];
    for (int i = 0; i < s1.length; i++) {
    try {
    n1[i] = Integer.parseInt(s1[i]);
    n2[i] = Integer.parseInt(s2[i]);
    if (i == 1) {
    if (n1[0] * 100 + n1[1] > n2[0] * 100 + n2[1]) {
    throw new Exception();
    }
    }
    } catch (Exception e) {
    TextView tv = new TextView(this);
    tv.setText("输入的日期有误!");
    new AlertDialog.Builder(this).setTitle("提示:").setView(tv)
    .setPositiveButton("确定", null).show();
    return;
    }
    }
    if (n2[1] < n1[1]) {
    minute = 60+n2[1] - n1[1];
    hour = n2[0] - n1[0] - 1;
    } else {
    hour = n2[0] - n1[0];
    minute = n2[1] - n1[1];
    }
    text.setText("倒计时:" + hour + "时" + minute + "分" + second + "秒");
    Timer timer = new Timer();
    timer.schedule(new TimerTask() { @Override
    public void run() {
    if (second == 0) {
    if (minute == 0) {
    if (hour == 0) {
    msg = new Message();
    msg.what = 1;
    handler.sendMessage(msg);
    } else {
    hour--;
    }
    minute = 59;
    } else {
    minute--;
    }
    second = 59;
    } else {
    second--;
    }
    msg = new Message();
    msg.what = 0;
    handler.sendMessage(msg); }
    }, 1000, 1000); }
    }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:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="请输入开始时间:(格式:hh mm)" />
    <EditText android:id="@+id/start" android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
    <TextView  android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="请输入结束时间:(格式:hh mm)" />
    <EditText android:id="@+id/end" android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
    <Button android:id="@+id/btn" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="开始倒计时" />
    <TextView android:id="@+id/text" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="" />
    </LinearLayout>
      

  2.   

    用个Timer直接实现就好了~~~LS的好强啊,全代码都给出了~~~++