代码之家  ›  专栏  ›  技术社区  ›  Eric Patterson

Android TimePickerDialog选择的时间未传递到实际的倒计时计时器

  •  0
  • Eric Patterson  · 技术社区  · 10 年前

    我一直在努力获取一个时间选择器对话框来设置我的倒计时计时器。我尝试了很多方法,但都没有成功。我决定尝试一些基本的代码,我将在下面介绍。

    我无法将所选时间(我在下面的代码中称之为selectedStartTime)作为startTime传递到实际的倒计时计时器中。我只能在使用预设的startTime=10000(或任何数字)时让计时器正常工作。我不希望startTime是一个固定的数字。我需要它来自时间选择器框中的OnTimeSetListener。

    我将永远感谢任何能向我展示如何更改代码的人,以便在对话框中选择的时间实际用于计时器。

    activity_simple_timer_test.XML的完整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>
    

    SimpleTimerTest.Java的完整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 回复  |  直到 10 年前
        1
  •  0
  •   dtx12    10 年前

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