本篇文章给大家分享的是有关Android中怎么实现计时与倒计时,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
方法一
Timer与TimerTask(Java实现)
public class timerTask extends Activity{ private int recLen = 11; private TextView txtView; Timer timer = new Timer(); public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); timer.schedule(task, 1000, 1000); // timeTask } TimerTask task = new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { // UI thread @Override public void run() { recLen--; txtView.setText(""+recLen); if(recLen < 0){ timer.cancel(); txtView.setVisibility(View.GONE); } } }); } }; }
方法二
TimerTask与Handler(不用Timer的改进型)
public class timerTask extends Activity{ private int recLen = 11; private TextView txtView; Timer timer = new Timer(); public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); timer.schedule(task, 1000, 1000); // timeTask } final Handler handler = new Handler(){ @Override public void handleMessage(Message msg){ switch (msg.what) { case 1: txtView.setText(""+recLen); if(recLen < 0){ timer.cancel(); txtView.setVisibility(View.GONE); } } } }; TimerTask task = new TimerTask() { @Override public void run() { recLen--; Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; }
方法三
Handler与Message(不用TimerTask)
public class timerTask extends Activity{ private int recLen = 11; private TextView txtView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); Message message = handler.obtainMessage(1); // Message handler.sendMessageDelayed(message, 1000); } final Handler handler = new Handler(){ public void handleMessage(Message msg){ // handle message switch (msg.what) { case 1: recLen--; txtView.setText("" + recLen); if(recLen > 0){ Message message = handler.obtainMessage(1); handler.sendMessageDelayed(message, 1000); // send message }else{ txtView.setVisibility(View.GONE); } } super.handleMessage(msg); } }; }
方法四
Handler与Thread(不占用UI线程)
public class timerTask extends Activity{ private int recLen = 0; private TextView txtView; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); new Thread(new MyThread()).start(); // start thread } final Handler handler = new Handler(){ // handle public void handleMessage(Message msg){ switch (msg.what) { case 1: recLen++; txtView.setText("" + recLen); } super.handleMessage(msg); } }; public class MyThread implements Runnable{ // thread @Override public void run(){ while(true){ try{ Thread.sleep(1000); // sleep 1000ms Message message = new Message(); message.what = 1; handler.sendMessage(message); }catch (Exception e) { }
方法五
Handler与Runnable(最简单型)
public class timerTask extends Activity{ private int recLen = 0; private TextView txtView; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); runnable.run(); } Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { recLen++; txtView.setText("" + recLen); handler.postDelayed(this, 1000); } }; }
以上就是Android中怎么实现计时与倒计时,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。