我試圖讓我的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