首页 > 代码库 > 采用数字控件的倒计时
采用数字控件的倒计时
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal" android:paddingLeft="3dp" > <NumberPicker android:id="@+id/np1" android:layout_width="50dp" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:textSize="20sp" android:text=":"/> <NumberPicker android:id="@+id/np2" android:layout_width="50dp" android:layout_height="wrap_content" android:layout_marginLeft="5dip"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:textSize="20sp" android:text=":"/> <NumberPicker android:id="@+id/np3" android:layout_width="50dp" android:layout_height="wrap_content"/> </LinearLayout> <TextView android:id="@+id/txt" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/showtiqian" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始倒计时" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" /> <TextView android:id="@+id/showcount" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>
package com.example.activity;import java.util.Calendar;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.NumberPicker;import android.widget.NumberPicker.OnValueChangeListener;import android.widget.TextView;import com.example.test.R;public class TimeCountActivity extends Activity{ private boolean STOP = true; private NumberPicker mSecondSpinner; private NumberPicker mHourSpinner; private NumberPicker mMinuteSpinner; private TextView timeCount; private Button btn_start ,btn1; private Calendar mDate; private int mHour, mMinute,mSecond; private Handler mHandler = new Handler();// 全局handler int time = 0;// 时间差 @SuppressLint("NewApi") protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.timecount); super.onCreate(savedInstanceState); mDate = Calendar.getInstance(); mHour = mDate.get(Calendar.HOUR_OF_DAY); mMinute = mDate.get(Calendar.MINUTE); mSecond = mDate.get(Calendar.SECOND); mHourSpinner = (NumberPicker) this.findViewById(R.id.np1); mHourSpinner.setMaxValue(23); mHourSpinner.setMinValue(0); mHourSpinner.setValue(mHour); mHourSpinner.setOnValueChangedListener(mOnHourChangedListener); mMinuteSpinner = (NumberPicker) this.findViewById(R.id.np2); mMinuteSpinner.setMaxValue(59); mMinuteSpinner.setMinValue(0); mMinuteSpinner.setValue(mMinute); mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener); mSecondSpinner = (NumberPicker) this.findViewById(R.id.np3); mSecondSpinner.setMaxValue(59); mSecondSpinner.setMinValue(0); mSecondSpinner.setValue(mSecond); mSecondSpinner.setOnValueChangedListener(mOnDateChangedListener); timeCount = (TextView) this.findViewById(R.id.showcount); btn_start = (Button) this.findViewById(R.id.btn_start); btn_start.setOnClickListener(new mClick()); btn1 = (Button) this.findViewById(R.id.button1); btn1.setOnClickListener(new mClick()); } class mClick implements OnClickListener { public void onClick(View v) { if(v == btn_start) { STOP = false; time = mSecond + mMinute*60 + mHour*3600; new Thread(new TimeCount()).start();// 开启线程 } else if(v == btn1) { STOP = true; } } } @SuppressLint("NewApi") private NumberPicker.OnValueChangeListener mOnDateChangedListener = new OnValueChangeListener() { @SuppressLint("NewApi") public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mSecond = mHourSpinner.getValue(); } }; @SuppressLint("NewApi") private NumberPicker.OnValueChangeListener mOnHourChangedListener = new OnValueChangeListener() { @SuppressLint("NewApi") public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mHour = mHourSpinner.getValue(); } }; @SuppressLint("NewApi") private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new OnValueChangeListener() { @SuppressLint("NewApi") public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mMinute = mMinuteSpinner.getValue(); } }; class TimeCount implements Runnable { @Override public void run() { while ( !STOP && time > 0)// 整个倒计时执行的循环 { time--; mHandler.post(new Runnable() // 通过它在UI主线程中修改显示的剩余时间 { public void run() { timeCount.setText(getInterval(time));// 显示剩余时间 } }); try { Thread.sleep(1000);// 线程休眠一秒钟 这个就是倒计时的间隔时间 } catch (InterruptedException e) { e.printStackTrace(); } } // 下面是倒计时结束逻辑 mHandler.post(new Runnable() { @Override public void run() { timeCount.setText("设定的时间到。"); } }); } } /** * 设定显示文字 */ public static String getInterval(int time) { String txt = null; if (time >= 0) { long hour = time % (24 * 3600) / 3600;// 小时 long minute = time % 3600 / 60;// 分钟 long second = time % 60;// 秒 txt =" 距离现在还有:" + hour + "小时" + minute + "分" + second + "秒"; } else { txt="已过期"; } return txt; }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。