我试图让我的textView每分钟或每两分钟出现在屏幕的不同位置(延迟并不重要)。我见过有人建议我使用runOnUiThread来让定时器重复随机函数并更新UI。使用runOnUiThread重复使用此函数来更新textview位置
我真的很挣扎让我的头绕着这些不同的线程,只是想知道是否有人可以给我一个例子?或者我应该研究使用不同的东西?
Public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.digitalClock1);
Random r = new Random();
int x = r.nextInt(350 - 100);
int y = r.nextInt(800 - 100);
textView.setX(x);
textView.setY(y);
}
你必须使用runOnUiThread更新您的TextView,但不要在此线程运行定时器()方法!使用新线程shedule或SheduledExecutorService:https://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html – luxer 2013-04-22 15:28:48