我剛開始學習創建android應用程序。我想創建一個簡單的向下計時器,它從edittext中獲取一個值,但倒數計時器似乎不運行。Android倒數計時器不會接受變量
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDownTxt = (TextView) findViewById(R.id.countDownView);
intervalTxt = (TextView) findViewById(R.id.intervalText);
findViewById(R.id.startBN).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
int testInt = 30;
//countDownTxt.setText(intervalTxt.getText());
int interval = Integer.parseInt(intervalTxt.getText().toString());
Log.d("buttonpressed", "interval for countdown is " + interval);
cdt = new CountDownTimer(Integer.parseInt(intervalTxt.getText().toString()), 1000) {
public void onTick(long millisUntilFinished) {
Log.d("counttimer1", "haha1");
countDownTxt.setText(""+ millisUntilFinished/1000);
}
public void onFinish() {
cancel();
}
}.start();
}
}
);
}
特別是,該程序只能如果我輸入一個數值,如「CDT =新CountDownTimer(testInt,1000),」 30000中CountDownTimer的第一個參數
能有人開導我,請?謝謝!
什麼是'Log'輸出? – Eric
那麼,它工作與否?你能告訴你logcat嗎?爲什麼不在'new CountDownTimer'中重複使用'interval'變量的值? –
嘗試'Long.parseLong(intervalTxt.getText()。toString())'而不是'Integer.parseInt(intervalTxt.getText()。toString())' –