0
我一直在努力獲取一個timepicker對話框來設置我的倒數計時器。我嘗試了很多方法,結果很多,都沒有成功。我決定嘗試一些我將在下面提到的基本代碼。Android的TimePickerDialog選定的時間沒有通過實際的倒數計時器
我無法獲得選定的時間(我在下面的代碼中將其稱爲selectedStartTime)作爲startTime傳入實際的倒數計時器。當我使用預設的startTime = 10000(或任何數字)時,我只能使計時器正常工作。我不想讓startTime成爲一個數字。我需要它來自時間選擇器框中的OnTimeSetListener。
我將永遠感謝任何能告訴我如何更改代碼的人,以便在對話框中實際使用定時器中的選定時間。
完整的XML代碼:activity_simple_timer_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dip">
<TableRow>
<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:text="Time: " />
<TextView
android:id="@+id/timeElapsed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:text="Time elapsed: " />
</TableRow>
</TableLayout>
</LinearLayout>
完整的Java代碼:SimpleTimerTest.java
package com.YOURPACKAGE INFO;
import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class SimpleTimerTest extends Activity implements OnClickListener {
private MalibuCountDownTimer countDownTimer;
private long timeElapsed;
private boolean timerHasStarted = false;
private Button startB;
private TextView text;
private TextView timeElapsedView;
int selectedStartTime;
// don't want to use a predefined startTime
private final long startTime = 10000;
private final long interval = 1000;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_timer_test);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
countDownTimer = new MalibuCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime));
}
TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
selectedStartTime = ((hourOfDay*3600000)+(minute*60000));
// I can't get this variable to pass through into the countdown timer
}
};
@Override
public void onClick(View v) {
TimePickerDialog d = new TimePickerDialog(SimpleTimerTest.this, onTimeSetListener, 1, 0, true);
d.setTitle("Pick Sleep Duration hour:min");
d.setCancelable(true);
d.setButton(DialogInterface.BUTTON_POSITIVE, "Start", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("Stop");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("Timer was already running");
}
}
}
});
d.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("Restart");
}
}
});
d.show();
}
public class MalibuCountDownTimer extends CountDownTimer {
public MalibuCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish() {
text.setText("Time's up!");
timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
Toast toast = Toast.makeText(getApplicationContext(), "finished", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("Time remain:" + millisUntilFinished);
timeElapsed = startTime - millisUntilFinished;
timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
}
}
}
我曾嘗試過。我遇到的問題是計時器不會取消。我永遠無法使countDownTimer.cancel()正常工作。我花了很長時間在那一個。我總是以一個不會取消的定時器或多個同時運行的定時器結束。我會試着重新考慮一下,看看我能否實現它,但我真的有問題。我只是希望有一種簡單的方法可以將選定的時間從方法中移出並存入計時器中。 –
然後只檢查計時器爲空,如果它已經創建,則先取消它。 – dtx12
嗯,它花了一整天,很多搞亂,但我設法把它放到onTimeSet方法,並得到它一切正常。我可能仍然看起來像一團糟,但至少它是一個混亂的工作。 –