how to set time of timepicker to countdowntimer?

2019-01-29 10:01发布

I am using timepicker to my app,now i am able to set time from timepicker and send it to next activity,now in next activity i am using countdown timer,now in my countdown timer i want to set that time and start timer,following is my code of timepicker and countdowntimer

 submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String timeofevent = edttmofevent.getText().toString();

                Intent intent = new Intent(AddNewEvent.this, EventDetails.class);
                intent.putExtra("times",timeofevent);
                startActivity(intent);
            }
        });



private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            int hour;
            //String am_pm;
            if (hourOfDay > 12) {
                hour = hourOfDay - 12;
               // am_pm = "PM";
            } else {
                hour = hourOfDay;
                //am_pm = "AM";
            }
            edttmofevent.setText(hour + " : " + minute + " ");
        }
    };

Countdowntimer

Intent intent=getIntent();
        tim=intent.getStringExtra("times");
 timerValue.setText(tim);
      //  final CounterClass timer = new CounterClass(Integer.parseInt(timerValue.getText().toString()), 1000);
        playbc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startTime = SystemClock.uptimeMillis();

                customHandler.postDelayed(updateTimerThread, 0);
               // timer.start();

            }
        });

        pausebc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);

               // timer.cancel();

            }
        });


    }

    private Runnable updateTimerThread = new Runnable() {
        public void run() {
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
            updatedTime = timeSwapBuff + timeInMilliseconds;
            int secs = (int) (updatedTime / 1000);

            int mins = secs / 60;
            int hours = mins/60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            timerValue.setText(""+ hours +":" + mins + ":"
                    + String.format("%02d", secs) + ":"
                    + String.format("%03d", milliseconds));
            customHandler.postDelayed(this, 0);

        }
    };

1条回答
小情绪 Triste *
2楼-- · 2019-01-29 10:52

Here hour1 and min1 are default selected time of dialog..

void setTimeInterval() {
        Calendar mcurrentTime = Calendar.getInstance();
        int hour1 = mcurrentTime.get(Calendar.HOUR_OF_DAY);
        int minute1 = mcurrentTime.get(Calendar.MINUTE);
        TimePickerDialog mTimePicker;
        mTimePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                // eReminderTime.setText(selectedHour + ":" +
                // selectedMinute);
                hour = selectedHour;
                minute = selectedMinute;
                if (hour > 0 || minute > 0) {
                    sendSettingsTimeInterval();
                }
            }

        }, hour1, minute1, true);// Yes 24 hour time
        mTimePicker.setTitle("Select Time Interval");
        mTimePicker.show();
    }
查看更多
登录 后发表回答