从timepickerdialog存储时间

时间:2015-07-29 18:18:35

标签: android

如何将timepickerdialog中的时间存储在android中作为单独的整数? 如果用户输入时间为20:00,我想将其存储为单独的整数22和00以供进一步使用。 我已经尝试了很多方法..即首先使用"的分割函数分割字符串:"然后对两个子字符串使用parseInt方法。但它不起作用。请给出有效的解决方案。

2 个答案:

答案 0 :(得分:0)

以下是我自定义TimePicker以将值分开的方式,以及基于它的计算AMPM值。

public void clicker (View view)
{
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    TimePickerDialog mTimePicker;
    mTimePicker = new TimePickerDialog(AddAlarm.this,
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker,
                                      int selectedHour, int selectedMinute) {
                    Calendar calendar = Calendar.getInstance();
                    //calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.set(Calendar.HOUR_OF_DAY, selectedHour);
                    calendar.set(Calendar.MINUTE, selectedMinute);

                    int c_hour,c_min;
                    String format;
                    c_hour=selectedHour;
                    c_min=selectedMinute;

                    if (c_hour == 0) {
                        c_hour += 12;
                        format = "AM";
                    } else if (c_hour == 12) {
                        format = "PM";
                    } else if (c_hour > 12) {
                        c_hour -= 12;
                        format = "PM";
                    } else {
                        format = "AM";
                    }

                    String formatTime = String.format("%02d : %02d %s", c_hour, c_min, format);

                    TextView dimple = (TextView)findViewById(R.id.timeText);
                    dimple.setText(formatTime);
                    setAlarm(calendar);

                }
            }, hour, minute, false);// Yes 24 hour time
    mTimePicker.setTitle("Select Time");
    mTimePicker.show();

答案 1 :(得分:-1)

class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            return new TimePickerDialog(getActivity(), this, hour, minute, false);
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // get hour and minute from here
        }
    }

通过

调用此timepicker对话框
new TimePickerFragment().show(getFragmentManager(), "starttime");