Android TimePickerDialog选择的时间不会传递给实际的倒计时器

时间:2015-09-08 21:07:34

标签: android timepicker countdowntimer

我一直致力于设置一个时间选择器对话框来设置我的倒数计时器。我尝试了许多不同结果的方法......它们都没有正常工作。我决定尝试一些基本代码,我将在下面介绍。

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

1 个答案:

答案 0 :(得分:0)

我不完全确定您要实现的目标,只需在方法MalibuCountDownTimer中创建onTimeSet的实例,只有在用户选择时间时才会调用此方法。