
有人可以给出一个每秒钟更新文本字段的简单示例吗?
我想做一个飞球,需要每秒计算/更新球坐标,这就是我需要某种计时器的原因.
我从here没有得到任何东西.
解决方法:
好吧,因为这还没有解决,有3种简单的方法来处理这个问题.
下面是一个示例,显示所有3和底部是一个示例,显示我认为更可取的方法.还记得在onPause中清理你的任务,必要时保存状态.
import java.util.Timer;import java.util.TimerTask;import androID.app.Activity;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.os.Handler.Callback;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.TextVIEw;public class main extends Activity { TextVIEw text, text2, text3; long starttime = 0; //this posts a message to the main thread from our timertask //and updates the textfIEld final Handler h = new Handler(new Callback() { @OverrIDe public boolean handleMessage(Message msg) { long millis = System.currentTimeMillis() - starttime; int seconds = (int) (millis / 1000); int minutes = seconds / 60; seconds = seconds % 60; text.setText(String.format("%d:%02d", minutes, seconds)); return false; } }); //runs without timer be reposting self Handler h2 = new Handler(); Runnable run = new Runnable() { @OverrIDe public voID run() { long millis = System.currentTimeMillis() - starttime; int seconds = (int) (millis / 1000); int minutes = seconds / 60; seconds = seconds % 60; text3.setText(String.format("%d:%02d", minutes, seconds)); h2.postDelayed(this, 500); } }; //tells handler to send a message class firstTask extends TimerTask { @OverrIDe public voID run() { h.sendEmptyMessage(0); } }; //tells activity to run on ui thread class secondTask extends TimerTask { @OverrIDe public voID run() { main.this.runOnUiThread(new Runnable() { @OverrIDe public voID run() { long millis = System.currentTimeMillis() - starttime; int seconds = (int) (millis / 1000); int minutes = seconds / 60; seconds = seconds % 60; text2.setText(String.format("%d:%02d", minutes, seconds)); } }); } }; Timer timer = new Timer(); @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); text = (TextVIEw)findVIEwByID(R.ID.text); text2 = (TextVIEw)findVIEwByID(R.ID.text2); text3 = (TextVIEw)findVIEwByID(R.ID.text3); button b = (button)findVIEwByID(R.ID.button); b.setText("start"); b.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { button b = (button)v; if(b.getText().equals("stop")){ timer.cancel(); timer.purge(); h2.removeCallbacks(run); b.setText("start"); }else{ starttime = System.currentTimeMillis(); timer = new Timer(); timer.schedule(new firstTask(), 0,500); timer.schedule(new secondTask(), 0,500); h2.postDelayed(run, 0); b.setText("stop"); } } }); } @OverrIDe public voID onPause() { super.onPause(); timer.cancel(); timer.purge(); h2.removeCallbacks(run); button b = (button)findVIEwByID(R.ID.button); b.setText("start"); }}要记住的主要事情是UI只能从主ui线程修改,所以使用处理程序或activity.runOnUIThread(Runnable r);
这是我认为首选的方法.
import androID.app.Activity;import androID.os.Bundle;import androID.os.Handler;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.TextVIEw;public class TestActivity extends Activity { TextVIEw timerTextVIEw; long startTime = 0; //runs without a timer by reposting this handler at the end of the runnable Handler timerHandler = new Handler(); Runnable timerRunnable = new Runnable() { @OverrIDe public voID run() { long millis = System.currentTimeMillis() - startTime; int seconds = (int) (millis / 1000); int minutes = seconds / 60; seconds = seconds % 60; timerTextVIEw.setText(String.format("%d:%02d", minutes, seconds)); timerHandler.postDelayed(this, 500); } }; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.test_activity); timerTextVIEw = (TextVIEw) findVIEwByID(R.ID.timerTextVIEw); button b = (button) findVIEwByID(R.ID.button); b.setText("start"); b.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { button b = (button) v; if (b.getText().equals("stop")) { timerHandler.removeCallbacks(timerRunnable); b.setText("start"); } else { startTime = System.currentTimeMillis(); timerHandler.postDelayed(timerRunnable, 0); b.setText("stop"); } } }); } @OverrIDe public voID onPause() { super.onPause(); timerHandler.removeCallbacks(timerRunnable); button b = (button)findVIEwByID(R.ID.button); b.setText("start"); }} 总结 以上是内存溢出为你收集整理的Android计时器?如何?全部内容,希望文章能够帮你解决Android计时器?如何?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)