0
我不知道什麼是錯的。 TextView不會改變它保持打印00. 我希望得到一個簡單的計時器,如0,1,2,.... 我想使用線程而不是像定時器或時間任務類。 謝謝你的時間。 下面的代碼:Android Studio中的計時器
public class MainActivity extends AppCompatActivity {
TextView time;
Button start,reset,exit;
Thread aspetta;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
time = (TextView)findViewById(R.id.tempo);
start = (Button)findViewById(R.id.inizio);
reset = (Button)findViewById(R.id.reset);
exit = (Button)findViewById(R.id.exit);
aspetta = new Thread();
i = 0;
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
waitAMinute(aspetta,time,i);
}
});
}
public void waitAMinute(Thread t,TextView tv,int x) {
x = 0;
while (true) {
try {
t.sleep(1000);
tv.setText(Integer.toString(x));
x++;
} catch(InterruptedException e) {
tv.setText("Error!");
}
}
}
}
我省略了之類的東西super.onCreate ......但這些都是我的源代碼。 – Giovanni
你也省略了這個事實,即Android Studio是IDE,並且完全不相關你用什麼編輯器編寫你的** **代碼** android ** –
你創建了一個線程,但是你的代碼實際上並沒有運行線。在我的手機上這麼短的答案對不起:-) – Blundell