利用BroadcastReceiver实现抢购倒计时,当然你也可以用Thread+Handler来完成。实际应用中我们倒计时的时间是从服务端获取的。
运行效果:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入倒计时(秒)" >
</EditText>
<Button
android:id="@+id/bt_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="倒计时开始"
android:onClick="begin"/>
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="抢购倒计时:"
android:textSize="20sp"
/>
</LinearLayout>
java代码:
package com.test.countdown;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class CountDownActivity extends Activity {
private EditText et_input;
private Button bt_start;
private TextView tv_time;
private long time;
private static final String DOWN="down";
private boolean flag;//线程循环标识
private StringBuilder sb;
private BroadcastReceiver receiver=new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals(DOWN))
{
if(time>0)
{
time=time-1;
}
if(time==0)
{
flag=false;//让线程停止
bt_start.setClickable(true);
}
long hour=time/3600;
if(hour<10){
sb.append(0);
}
sb.append(hour);
sb.append(":");
long minute=time%3600/60;
if(minute<10){
sb.append(0);
}
sb.append(minute);
sb.append(":");
long second=time%3600%60;
if(second<10){
sb.append(0);
}
sb.append(second);
tv_time.setText("抢购倒计时:"+sb.toString());
sb.delete(0, sb.length());//删除StringBulider里面的内容
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sb=new StringBuilder();
setView();
//注册广播事件
IntentFilter filter=new IntentFilter("down");
registerReceiver(receiver, filter);
}
private void setView()
{
et_input=(EditText) findViewById(R.id.et_input);
bt_start=(Button) findViewById(R.id.bt_start);
tv_time=(TextView) findViewById(R.id.tv_time);
}
public void begin(View view)
{
String content=et_input.getText().toString().trim();
System.out.println(content);
if(TextUtils.isEmpty(content))
{
Toast.makeText(this, "输入不能为空,请输入时间(秒)!", 0).show();
return;
}
if(!TextUtils.isDigitsOnly(content))
{
Toast.makeText(this, content+"是错误的时间!", 0).show();
et_input.setText("");
return;
}
time=Long.parseLong(content);
flag=true;
bt_start.setClickable(false);//按钮在开始倒计时后不能被点击
//开辟一个子线程每隔一秒发送一下广播
new Thread(){
public void run() {
while(flag){
try
{
sleep(1000);
Intent intent=new Intent("down");
sendBroadcast(intent);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
}.start();
}
@Override
protected void onDestroy()
{
unregisterReceiver(receiver);
flag=false;
super.onDestroy();
}
}