我希望我的切換按鈕在一段時間後取消選中,當我檢查它時。我使用Timer和TimerTask,但當我嘗試執行此操作時,我的應用程序始終強制關閉。這是代碼:在一段時間後取消選中切換按鈕
public class Vjezba5Activity extends Activity {
TimerTask TimerTask11;
Timer timer1 = new Timer();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ToggleButton TB1 = (ToggleButton) findViewById(R.id.toggleButton1);
TB1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (TB1.isChecked()){
TimerTask11 = new TimerTask() {
public void run() {
TB1.setChecked(false);
}
};
timer1.schedule(TimerTask11, 1000);
}
else {
Toast.makeText(Vjezba5Activity.this, "Uncheck", Toast.LENGTH_SHORT).show();
}
}
});
}
我會使用一個處理程序。然後發佈涉及取消選中按鈕的操作 – AJcodez 2012-01-30 01:15:09