Android CountDownTimer倒计时简单使用
Schedule a countdown until a time in the future, with regular notifications on intervals along the way.
倒计时这个类比较简单,可以学习这样来设计类,下面看这个类的一个简单应用的例子:
这么个小例子也需要优化,优化之前什么也没考虑只实现了效果, 简单优化下增加了了多次点击Dlialog不重复创建和取消countdowntimer任务的代码;
简单优化后:
package com.example.dliagdemo; import android.app.Activity; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.CountDownTimer; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button btnSure; private MainActivity instance; private CountDownTimer mDownTimer; private Dialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); instance = this; Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (dialog == null) showDialog(); else if (!dialog.isShowing()) showDialog(); } }); } private void showDialog() { dialog = new Dialog(instance); LayoutInflater inflater = LayoutInflater.from(instance); View v = inflater.inflate(R.layout.itmes, null); btnSure = (Button) v.findViewById(R.id.buttonSure); btnSure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(instance, "游戏开始", Toast.LENGTH_SHORT).show(); if (dialog != null) { dialog.dismiss(); } } }); mDownTimer = new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { if (btnSure != null) { btnSure.setText("自动开始(" + millisUntilFinished / 1000 + "/s)"); } } public void onFinish() { if (btnSure != null) { btnSure.performClick(); } } }.start(); dialog.setContentView(v); dialog.setTitle("开始游戏"); dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { if (mDownTimer != null) { mDownTimer.cancel(); mDownTimer = null; } } }); dialog.show(); } }
只简单实现倒计时的效果。
package com.example.dliagdemo; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.os.CountDownTimer; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button btnSure; private MainActivity instance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); instance =this; Button btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(); } }); } private void showDialog() { final Dialog dialog = new Dialog(instance); LayoutInflater inflater = LayoutInflater.from(instance); View v =inflater.inflate(R.layout.itmes, null); btnSure = (Button)v.findViewById(R.id.buttonSure); btnSure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(instance, "游戏开始", Toast.LENGTH_SHORT).show(); if (dialog!=null&&dialog.isShowing()) { dialog.dismiss(); } } }); new CountDownTimer(10000, 1000) {//总时间, 间隔时间 public void onTick(long millisUntilFinished) { btnSure.setText("即将开始(" + millisUntilFinished / 1000+"/s)"); } public void onFinish() { if (dialog!=null&&dialog.isShowing()) { btnSure.performClick();//点击按钮 dialog.dismiss(); } } }.start(); dialog.setContentView(v); dialog.setTitle("开始游戏"); dialog.show(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。