我试着每1分钟显示一个msg!不停!我发现例外,一个固定的延迟后显示msg一次!你可以帮忙怎么设置呢?或者如果使用定时器是更好的工作,我需要一个例子!
public class TimertestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Handler handler = new Handler(); handler.postDelayed( new Runnable() { public void run() { afficher(); } },1000L); } public void afficher() { Toast.makeText(getBaseContext(),"test",Toast.LENGTH_SHORT).show(); } }
谢谢!
解决方法
尝试这段代码 –
public class TimertestActivity extends Activity { Handler handler = new Handler(); Runnable runnable = new Runnable() { public void run() { afficher(); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); runnable.run(); } public void afficher() { Toast.makeText(getBaseContext(),Toast.LENGTH_SHORT).show(); handler.postDelayed(runnable,1000); } }