从Date&中选择日期和时间后时间选择器,如何将它们作为参数传递给日历的“创建事件”功能?

时间:2016-09-04 12:06:54

标签: android android-calendar android-datepicker android-timepicker

我有一个日期& Android Studio的Time Picker小部件,我从中选择了特定日期(dd-mm-yy)格式和时间(hr:min)格式。

现在,在同一个Activity我有一个'提交'按钮;通过点击它,我将几个参数传递给我手机的原生日历的'创建事件'。

虽然我可以成功传递标题,描述,位置等。我不知道如何通过'开始结束日期'&我从“日期和时间”中选择的“开始结束”时间参数时间选择器到原生日历。

感谢。

这里是一个简短的代码:

1>选择日期功能:

private void selectDate()
{
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dpd = new DatePickerDialog(getActivity(),
            new DatePickerDialog.OnDateSetListener()
            {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {
                    Toast.makeText(getActivity(), dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Toast.LENGTH_LONG).show();
                }
            }, mYear, mMonth, mDay);
    dpd.show();
}

2 - ;选择时间功能:

private void selectTime()
{
    final Calendar c = Calendar.getInstance();
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);

    TimePickerDialog tpd = new TimePickerDialog(getActivity(),
            new TimePickerDialog.OnTimeSetListener()
            {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute)
                {
                    Toast.makeText(getActivity(), hourOfDay + ":" + minute, Toast.LENGTH_LONG).show();
                }
            }, mHour, mMinute, false);
    tpd.show();
}      

3)在提交按钮上单击:

Sb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        Intent schedule = new Intent(Intent.ACTION_INSERT);
        schedule.setType("vnd.android.cursor.item/event");
        schedule.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,   startTime);
        schedule.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime);
        schedule.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false);
        schedule.putExtra(CalendarContract.Events.TITLE, "Title");
        schedule.putExtra(CalendarContract.Events.EVENT_LOCATION, "Location");
        startActivity(schedule);
        }
    });

我已编辑了我的代码以提供建议:

private void selectTime()
{
    TimePickerDialog tpd = new TimePickerDialog(getActivity(),
            new TimePickerDialog.OnTimeSetListener()
            {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute)
                {
                     c.set(Calendar.HOUR_OF_DAY, hourOfDay);
                     c.set(Calendar.MINUTE, minute);

                    Toast.makeText(getActivity(), hourOfDay + ":" + minute, Toast.LENGTH_LONG).show();
                }
            }, mHour, mMinute, false);
    tpd.show();
}

 private void selectDate()
{
    DatePickerDialog dpd = new DatePickerDialog(getActivity(),
            new DatePickerDialog.OnDateSetListener()
            {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {
                    c.set(year, monthOfYear, dayOfMonth);

                    Toast.makeText(getActivity(), dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Toast.LENGTH_LONG).show();
                }
            }, mYear, mMonth, mDay);
    dpd.show();
}

现在,时间&每次创建新活动时,日期都会完美更改/更新!谢谢

3 个答案:

答案 0 :(得分:1)

您可以制作Calendar calendar = Calendar.getInstance(); 实例变量:

onDateSet()

onTimeSet()@Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { calendar.set(year, monthOfYear, dayOfMonth); } // ... @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); } 设置其值:

Calendar

然后将此schedule.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, calendar.getTimeInMillis()); 实例的值从epoch传递为millis:

global.jQuery = require('jquery');
require('tipso');

答案 1 :(得分:0)

在活动中制作日期对象

private Date date = null;

在构造函数中:

date = new Date();

onDateSet

date.setYear(year);
date.setMonth(monthOfYear);
date.setDate(dayOfMonth);
onTimeSet

中的

date.setHours(hourOfDay);
date.setMinutes(minute);

然后您可以在提交功能

中使用此日期

答案 2 :(得分:0)

看起来有点老但你可以尝试这个http://www.vogella.com/tutorials/AndroidCalendar/article.html 它将GregorianCalendar参数传递给api。

否则转到文档https://developer.android.com/guide/topics/providers/calendar-provider.html使用意图插入事件部分中的示例。

Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
...

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);