Android TimePickerDialog 选择的时间没有传递到实际的倒数计时器

Android TimePickerDialog selected time not passing through to the actual Countdown Timer

我一直在努力获得一个时间选择器对话框来设置我的倒数计时器。我尝试了很多方法并得到了很多不同的结果... none 其中的方法是正确的。我决定试验一些我将在下面给出的基本代码。

我无法将选定的时间(我在下面的代码中将其称为 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));
    }
}
} 

我不完全确定你想要实现什么,只需在方法 onTimeSet 中创建你的 MalibuCountDownTimer 的实例,此方法将仅在用户选择时间时调用。