一个简单的自定义的观点。在里面封装了时间的倒计时,以及距离现在时间的时间计算
公开课TimerTextView LinearLayout{延伸//时间变量 私人长第二; 私人TextView tv_Time; 私人TextView tv_Unit; RefreshCallBack RefreshCallBack; 公共TimerTextView(上下文语境、AttributeSet attrs int defStyleAttr) { 超级(上下文、attrs defStyleAttr); initView(上下文); } 公共TimerTextView(上下文语境,AttributeSet attrs) { 超级(上下文,attrs); initView(上下文); } 公共TimerTextView(上下文语境){ 超级(上下文); initView(上下文); } 私人空间initView(上下文语境){//加载布的局 LayoutInflater.from(上下文).inflate (R.layout.timer_text_view,); tv_Time=(TextView) findViewById (R.id.countdown_time); tv_Unit=(TextView) findViewById (R.id.countdown_unit); } @Override 保护无效onDetachedFromWindow () { super.onDetachedFromWindow ();//在控件被销毁时移除消息 handler.removeMessages (0); } 私人布尔isRun=true;//是否启动了 私人处理程序处理程序=new处理程序(Looper.getMainLooper ()) { @Override 公共空间handleMessage(消息味精){ 开关(msg.what) { 例0: 如果(isRun) { 如果(第二比;0){ 第二个=第二- 1; 处理程序。sendEmptyMessageDelayed (0, 1000); 其他}{ 如果(零!=refreshCallBack) { refreshCallBack.refreshCallBack(真正的); isRun=false; } } } 打破; } } }; 公共布尔isRun () { 返回isRun; }/* * *开始计时 */公共空间开始(){ isRun=true; handler.removeMessages (0); handler.sendEmptyMessage (0); }/* * *结束计时 */公共空间停止(){ isRun=false; } 公共空间diffTime(字符串endTime) { SimpleDateFormat自卫队=new SimpleDateFormat (“yyyy-MM-dd HH: mm: ss Locale.CHINA); 字符串的开始时间=自卫队。格式(新日期()); 字符串格式=" yyyy-MM-dd hh: mm: ss”;//按照传入的格式生成一个simpledateformate对象 SimpleDateFormat sd=new SimpleDateFormat(格式); 长nd=1000 * 24 * 60 * 60;//一天的毫秒数 长nh=1000 * 60 * 60;//一小时的毫秒数 长海里=1000 * 60;//一分钟的毫秒数 长ns=1000;//一秒钟的毫秒数长diff;尝试{//获得两个时间的毫秒时间差异 长diff=0; 尝试{ diff=sd.parse (endTime) .getTime ()——sd.parse(开始时间).getTime (); }捕捉(ParseException e) { e.printStackTrace (); } 如果(diff & lt;0){ 如果(零!=refreshCallBack) { refreshCallBack.showCallBack(假); } 返回; 其他}{ 如果(零!=refreshCallBack) { refreshCallBack.showCallBack(真正的); } 漫长的一天=diff/nd;//计算差多少天 如果(天比;0){ tv_Time.setText (String.valueOf(天)); tv_Unit.setText(“天”); 其他}{ 长时间=diff % nd/nh;//计算差多少小时 如果(小时比;0){ tv_Time.setText (String.valueOf(小时)); tv_Unit.setText(“小时”); 其他}{ 长min=diff %和% nh/nm;//计算差多少分钟 如果(最低的在0){ tv_Time.setText (String.valueOf (min)); tv_Unit.setText(“分钟”); 其他}{ 第二个=diff %和% nh % nm/ns;//计算差多少秒//输出结果//如果(最低的在0) {//stringBuffer.append (sec +“秒”);//} handler.removeMessages (0); handler.sendEmptyMessage (0); tv_Unit.setText(即将开始”); tv_Time.setVisibility(了); } } } } } 公共空间setTextViewSize (int大小){ 如果(零!=tv_Time) { tv_Time.setTextSize(大小); } 如果(零!=tv_Unit) { tv_Unit.setTextSize(大小); } } 公共空间setTextViewSpace (String类型){ 如果(“大”.equals(类型)){ LinearLayout。LayoutParams lp2=(LayoutParams) tv_Time.getLayoutParams (); lp2。setMargins (0, 0, DensityUtil.dip2px (tv_Time.getContext (), 12), 0); tv_Time.setLayoutParams (lp2); tv_Time.setBackground (getresource () .getDrawable (R.drawable.bg_video_count_down)); }else if(“中产”.equals(类型)){ tv_Time。setPadding (12 0 12,0); LinearLayout。LayoutParams lp2=(LayoutParams) tv_Time.getLayoutParams (); lp2。setMargins(0, 0, 12日0); tv_Time.setLayoutParams (lp2); 其他}{ tv_Time。setPadding (8 0 8,0); LinearLayout。LayoutParams lp2=(LayoutParams) tv_Time.getLayoutParams (); lp2。setMargins (0, 0 8 0); tv_Time.setLayoutParams (lp2); } } 公共空间setRefreshCallBack (RefreshCallBack RefreshCallBack) { 这一点。refreshCallBack=refreshCallBack; } 公共接口RefreshCallBack { 公共空间refreshCallBack(布尔标志); 公共空间showCallBack(布尔标志); } }Android自定义观点倒计时60秒