Android倒计时时间控件
在做一些电商或购物的app时,我们经常会看到倒计时抢购的标示,今天恰有时间把项目中用到的该控件提取出来,有需要的可以参考下
======================================================================================================
版权所有,如需转载请标明出处:http://blog.csdn.net/you4580
=======================================================================================================
<?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:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ele_surplus"
android:textColor="#999999"
android:textSize="14sp" />
<TextView
android:id="@+id/electricity_time_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="@drawable/electricity_countdown"
android:gravity="center"
android:minWidth="24dp"
android:padding="3dp"
android:textColor="#1A7FCF"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天"
android:textColor="#999999"
android:textSize="14sp" />
<TextView
android:id="@+id/electricity_time_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="@drawable/electricity_countdown"
android:gravity="center"
android:minWidth="24dp"
android:padding="3dp"
android:textColor="#1A7FCF"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时"
android:textColor="#999999"
android:textSize="14sp" />
<TextView
android:id="@+id/electricity_time_min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="@drawable/electricity_countdown"
android:gravity="center"
android:minWidth="24dp"
android:padding="3dp"
android:textColor="#1A7FCF"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分"
android:textColor="#999999"
android:textSize="14sp" />
<TextView
android:id="@+id/electricity_time_mse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="@drawable/electricity_countdown"
android:gravity="center"
android:minWidth="24dp"
android:padding="3dp"
android:textColor="#1A7FCF"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="秒"
android:textColor="#999999"
android:textSize="14sp" />
</LinearLayout>
======================================================================================================
TimeTextView 倒计时控件
@DESCRIPTION:
@AUTHOR:you4580
@VERSION:v1.0
@DATE:2015-4-20
/
public class TimeTextView extends LinearLayout{
private Context mContext;
private TextView mDay,mHour,mMin,mMse;
private long days,hours,minutes,seconds;
private boolean run = true; //是否启动了
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
if (run) {
if(computeTime()){
setUi();
handler.sendEmptyMessageDelayed(0, 1000);
}
}
break;
}
}
};
public TimeTextView(Context context){
super(context);
this.mContext = context;
inflateLayout();
};
public TimeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
inflateLayout();
}
@SuppressLint("NewApi")
public TimeTextView(Context context, AttributeSet attrs,int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
inflateLayout();
}
private void inflateLayout(){
this.addView(LayoutInflater.from(mContext).inflate(R.layout.time_textview, null));
mDay = (TextView) this.findViewById(R.id.electricity_time_day);
mHour = (TextView) this.findViewById(R.id.electricity_time_hour);
mMin = (TextView) this.findViewById(R.id.electricity_time_min);
mMse = (TextView) this.findViewById(R.id.electricity_time_mse);
}
public void setTime(String day,String hour,String minute,String second){
if(TextUtils.isEmpty(day)){
days = 0;
}else{
days = Long.parseLong(day);
}
if(TextUtils.isEmpty(hour)){
hours = 0;
}else{
hours = Long.parseLong(hour);
}
if(TextUtils.isEmpty(minute)){
minutes = 0;
}else{
minutes = Long.parseLong(minute);
}
if(TextUtils.isEmpty(second)){
seconds = 0;
}else{
seconds = Long.parseLong(second);
}
setUi();
handler.removeMessages(0);
//每隔1秒钟发送一次handler消息
handler.sendEmptyMessageDelayed(0,1000);}
private void setUi(){
mDay.setText(String.valueOf(days));
mHour.setText(String.valueOf(hours));
mMin.setText(String.valueOf(minutes));
mMse.setText(String.valueOf(seconds));
}
private boolean computeTime(){
boolean flag = true;
seconds--;
if (seconds < 0) {
minutes--;
seconds = 59;
if (minutes < 0) {
minutes = 59;
hours--;
if (hours < 0) {
hours = 23;
days--;
if(days<0){
flag = false;
}
}
}
}
return flag;
}
public void stopComputeTime(){
run = false;
}
}
==================================================================================================
调用自定义控件的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.timetextview.MainActivity" >
<com.example.timetextview.view.TimeTextView
android:id="@+id/electricity_countdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
===================================================================================================
public class MainActivity extends ActionBarActivity {
private TimeTextView mTimeTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTimeTextView=(TimeTextView) findViewById(R.id.electricity_countdown);
mTimeTextView.setTime("5","00","00","00");
}
}