使用时间选择器对话框android设置倒数计时器

时间:2015-12-07 01:09:22

标签: android android-studio timepicker

我的最终目标是使用android中的时间选择器对话框设置倒数计时器。截至目前,我有一个时间选择器可以设置为任何时间,我有一个倒计时器。所以我的问题是如何将倒数计时器中的textview设置为我的时间选择器中设置的内容。在我的代码中,我只将倒数计时器设置为三分钟,但我希望将其设置为时间选择器对话框设置时间。

这是我的......

public class SleepTimer extends AppCompatActivity {
TimePicker myTimePicker;
Button buttonstartSetDialog;
TextView textAlarmPrompt;
TimePickerDialog timePickerDialog;
final static int RQS_1 = 1;
Button btnStart, btnStop;
TextView textViewTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sleep_timer);

    btnStart = (Button)findViewById(R.id.btnStart);
    btnStop= (Button)findViewById(R.id.btnStop);
    textViewTime = (TextView)findViewById(R.id.textViewTime);



    final CounterClass timer = new CounterClass(180000, 1000);

    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            timer.start();
        }
    });
    btnStop.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            timer.cancel();
        }
    });

    textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);
    buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
    buttonstartSetDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textAlarmPrompt.setText("");
            openTimePickerDialog(false);
        }
    });


}

private void openTimePickerDialog(boolean is24r){
    Calendar calendar = Calendar.getInstance();

    timePickerDialog = new TimePickerDialog(
            SleepTimer.this,
            onTimeSetListener,
            calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE),
            is24r);
    timePickerDialog.setTitle("Set Sleep Time");

    timePickerDialog.show();
}

TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        Calendar calNow = Calendar.getInstance();
        Calendar calSet = (Calendar) calNow.clone();

        calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calSet.set(Calendar.MINUTE, minute);
        calSet.set(Calendar.SECOND, 0);
        calSet.set(Calendar.MILLISECOND, 0);

        if(calSet.compareTo(calNow) <= 0){

            calSet.add(Calendar.DATE, 1);
        }

        setAlarm(calSet);

    }};


public class CounterClass extends CountDownTimer {

    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */

    public CounterClass(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onTick(long millisUntilFinished) {
        long millis = millisUntilFinished;
        String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
        System.out.println(hms);
        textViewTime.setText(hms);

    }

    @Override
    public void onFinish() {

    }
}

如果有人知道怎么做,请回复。感谢:)

1 个答案:

答案 0 :(得分:4)

首先更改此方法,使其看起来像这样

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        timer = new CounterClass((minute * 60 * 1000) + (hourOfDay * 60* 60 * 1000), 1000);
        long millis = (minute * 60 * 1000) + (hourOfDay * 60* 60 * 1000);
        String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
        System.out.println(hms);
        textViewTime.setText(hms);

    }};

然后删除final CounterClass timer = new CounterClass(180000, 1000) 并将其添加为字段而不是

CounterClass timer;

此代码设置TextView,并使用选择的时间更新您的计数器。但我重用了你的代码,现在我们有相同的代码重复,这是不好的。将其提取为一种方法,这样我们就不会重复了。

COPY PASTE EDITION

public class SleepTimer extends AppCompatActivity {
TimePicker myTimePicker;
Button buttonstartSetDialog;
TextView textAlarmPrompt;
TimePickerDialog timePickerDialog;
final static int RQS_1 = 1;
Button btnStart, btnStop;
TextView textViewTime;
CounterClass timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sleep_timer);

    btnStart = (Button) findViewById(R.id.btnStart);
    btnStop = (Button) findViewById(R.id.btnStop);
    textViewTime = (TextView) findViewById(R.id.textViewTime);




    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            timer.start();
        }
    });
    btnStop.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            timer.cancel();
        }
    });

    textAlarmPrompt = (TextView) findViewById(R.id.alarmprompt);
    buttonstartSetDialog = (Button) findViewById(R.id.startSetDialog);
    buttonstartSetDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textAlarmPrompt.setText("");
            openTimePickerDialog(false);
        }
    });


}

private void openTimePickerDialog(boolean is24r) {
    Calendar calendar = Calendar.getInstance();

    timePickerDialog = new TimePickerDialog(
            SleepTimer.this,
            onTimeSetListener,
            calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE),
            is24r);
    timePickerDialog.setTitle("Set Sleep Time");

    timePickerDialog.show();
}

TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    timer = new CounterClass((minute * 60 * 1000) + (hourOfDay * 60* 60 * 1000), 1000);
    long millis = (minute * 60 * 1000) + (hourOfDay * 60* 60 * 1000);
    String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
            TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
    System.out.println(hms);
    textViewTime.setText(hms);

}};


public class CounterClass extends CountDownTimer {

    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */

    public CounterClass(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onTick(long millisUntilFinished) {
        long millis = millisUntilFinished;
        String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
        System.out.println(hms);
        textViewTime.setText(hms);

    }

    @Override
    public void onFinish() {

    }
}