Android实现时间倒计时功能
来源:爱站网时间:2020-08-20编辑:网友分享
Android定制倒计时的控件,这个功能需要设置开始时间和结束时间,再进行倒数,也可以直接设置倒计时启用,那么Android实现时间倒计时功能大家都了解吗?别着急,下面跟着爱站技术频道小编一起来学习吧!
Android定制倒计时的控件,这个功能需要设置开始时间和结束时间,再进行倒数,也可以直接设置倒计时启用,那么Android实现时间倒计时功能大家都了解吗?别着急,下面跟着爱站技术频道小编一起来学习吧!
效果展示
MainActivity(主页面代码)
public class MainActivity extends Activity { private RelativeLayout countDown; // 倒计时 private TextView daysTv, hoursTv, minutesTv, secondsTv; private long mDay = 10; private long mHour = 10; private long mMin = 30; private long mSecond = 00;// 天 ,小时,分钟,秒 private boolean isRun = true; private Handler timeHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==1) { computeTime(); daysTv.setText(mDay+""); hoursTv.setText(mHour+""); minutesTv.setText(mMin+""); secondsTv.setText(mSecond+""); if (mDay==0&&mHour==0&&mMin==0&&mSecond==0) { countDown.setVisibility(View.GONE); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countDown = (RelativeLayout) findViewById(R.id.countdown_layout); daysTv = (TextView) findViewById(R.id.days_tv); hoursTv = (TextView) findViewById(R.id.hours_tv); minutesTv = (TextView) findViewById(R.id.minutes_tv); secondsTv = (TextView) findViewById(R.id.seconds_tv); startRun(); } /** * 开启倒计时 */ private void startRun() { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (isRun) { try { Thread.sleep(1000); // sleep 1000ms Message message = Message.obtain(); message.what = 1; timeHandler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } } } }).start(); } /** * 倒计时计算 */ private void computeTime() { mSecond--; if (mSecond
main(主页面布局)
综上所述是爱站技术频道小编为大家带来的Android实现时间倒计时功能,不管是哪种开发,都需要适度进行后才能看出效果。