我有一個工作倒計時,但它只從我輸入的硬編碼整數開始倒計數。我希望用戶能夠鍵入一個數字並讓它倒數從那個數字改爲。我希望將放入「timeedit」的文本放入一個字符串並放到「startTime」的值中。從用戶定義的時間量倒計時
編輯:如果下面的代碼是不是你的屏幕上的縮進格式正確,您還可以查看這裏的代碼:http://pastebin.com/BnzEtFX5
代碼:
public class TimerActivity extends Activity implements OnClickListener {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView text;
public String time;
private long startTime = 30 * 1000;
private final long interval = 1 * 1000;
private EditText timeedit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countdown);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
timeedit = (EditText) findViewById(R.id.timeedit);
countDownTimer = new MyCountDownTimer(startTime, interval);
time = timeedit.getText().toString();
text.setText(time); //+ String.valueOf(startTime/1000)
}
@Override
public void onClick(View v) {
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startTime = 30 * 1000;
startB.setText("RESTART");
}
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
text.setText("Time's up!");
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished/1000);
}
}
}
哇,你的代碼縮進怎麼了?這真的很難閱讀。 –
@GregHewgill:IntelliJ中的CTRL + ALT + L,問題已解決。 :P – Makoto
@Makoto:當然,對你而言。但是,我已經看到人們實際上嘗試寫入代碼而沒有縮進,而OP需要知道這並不好。 –