在做一些电商或购物的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>
======================================================================================================
handler.removeMessages(0);
//每隔1秒钟发送一次handler消息
handler.sendEmptyMessageDelayed(0,1000);}
==================================================================================================
调用自定义控件的布局文件
<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");
}
}
=======================================================================================================================
原文:http://blog.csdn.net/you4580/article/details/45339871