1
我想做一個簡單的計時器。簡單的提醒爲Android
package com.anta40.reminder;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost.TabSpec;
public class Reminder extends Activity{
public final int TIMER_DELAY = 1000;
public final int TIMER_ONE_MINUTE = 60000;
public final int TIMER_ONE_SECOND = 1000;
Timer timer;
TimerTask task;
TextView tv;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
tv = (TextView) findViewById(R.id.textview1);
tv.setText("BOOM!!!!");
tv.setVisibility(TextView.VISIBLE);
try {
this.wait(TIMER_DELAY);
}
catch (InterruptedException e){
}
tv.setVisibility(TextView.INVISIBLE);
}
};
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabSpec spec = tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("Clock");
tabs.addTab(spec);
spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Settings");
tabs.addTab(spec);
tabs.setCurrentTab(0);
RadioGroup rgroup = (RadioGroup) findViewById(R.id.rgroup);
rgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.om){
timer.schedule(task, TIMER_DELAY, 3*TIMER_ONE_SECOND);
}
else if (checkedId == R.id.twm){
timer.schedule(task, TIMER_DELAY, 6*TIMER_ONE_SECOND);
}
else if (checkedId == R.id.thm){
timer.schedule(task, TIMER_DELAY, 9*TIMER_ONE_SECOND);
}
}
});
}
}
每次我點擊一個單選按鈕,計時器應該開始了吧? 但爲什麼不啓動?
>>它們在您將控制權歸還給Android之後纔會生效 那麼我該如何返回它?第三,如果您確實將代碼移動到主應用程序線程中,請不要阻止該線程,例如使用wait()調用。 在給定的時間間隔之後,我希望textview「Boom」不可見。因此,wait()。目前我不知道更好的解決方案。 – anta40 2010-05-27 02:31:20
「那我該怎麼回事?」通過返回(即退出所有回撥)。 「在給定的時間間隔之後,我希望textview」Boom「是不可見的,因此,wait(),我現在還不知道更好的解決方案。」在View或Handler上使用'postDelayed()'來安排一個'Runnable'在你想要的延遲時間後運行,並使'TextView'在'Runnable'的run()方法中不可見。 – CommonsWare 2010-05-27 11:23:36