2
StartTimer()函數應在按鈕點擊事件完成後調用,但在onclick事件之前執行。 ro如何在onclick事件之前停止計時器並在onclick事件之後啓動。StartTimer()應在按鈕點擊事件完成後調用,但在onclick事件之前執行
我該如何解決這個問題?
btn1=(Button)findViewById(R.id.button);
btn2=(Button)findViewById(R.id.button2);
btn3=(Button)findViewById(R.id.button3);
timer = new Timer();
btn1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button1", Toast.LENGTH_SHORT).show();
}
});
StartTimer();
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button2", Toast.LENGTH_SHORT).show();
}
});
StartTimer();
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button3", Toast.LENGTH_SHORT).show();
}
});
}
public void StartTimer()
{
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Log.v("timer", "Timer running");
}
}, 0, 5000);
Toast.makeText(getApplicationContext(), "Timer", Toast.LENGTH_LONG).show();
}
protected void onResume() {
super.onResume();
StartTimer();
}
}
謝謝Mr.Driver – Aruna
@Aruna你在我的暱稱中犯了2個錯誤 – Divers
對不起,我沒有注意到它。 – Aruna