简体   繁体   English

如何在Android 4.0默认日历中添加事件?

[英]How to add events in Android 4.0 default calendar?

在此输入图像描述

In Android 4.0 default calendar if we want to move to next month then we need to scroll the calendar but I need to move to the next or previous month by pressing arrows existed in the above image. 在Android 4.0默认日历中,如果我们想要移动到下个月,那么我们需要滚动日历,但我需要通过按上面图像中存在的箭头移动到下个月或上个月。 And also I want to add to the default calendar. 而且我想添加到默认日历。 If add the event then that date should be marked like in the image. 如果添加事件,那么该日期应该在图像中标记。

I used this CalendarView. 我用过这个CalendarView。

<CalendarView
    android:id="@+id/calendarView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

I want to know how to use add event to this CalendarView pragmatically. 我想知道如何实际使用添加事件到这个CalendarView。

To move to next or previous month you can use this code: 移至下个月或上个月,您可以使用以下代码:

    mButtonNext = (Button) findViewById(R.id.buttonNext);
    mButtonNext.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar cal = new GregorianCalendar();
            cal.setTimeInMillis(mCalendarView.getDate() );
            cal.add(Calendar.MONTH, 1);
            mCalendarView.setDate(cal.getTimeInMillis(), true, true);
        }
    });

    mButtonPrevious = (Button) findViewById(R.id.buttonPrevious);
    mButtonPrevious.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar cal = new GregorianCalendar();
            cal.setTimeInMillis(mCalendarView.getDate() );
            cal.add(Calendar.MONTH, -1);                
            mCalendarView.setDate(cal.getTimeInMillis(), true, true);
        }
    });

If you want to add an event in the default calendar , you have to see the link @adam2510 posted. 如果您想在默认日历中添加活动,则必须看到@ adam2510发布的链接。 Here is a sample code you can use: 以下是您可以使用的示例代码:

public void addEvent() {   
    ContentResolver cr = mContext.getContentResolver();
    ContentValues eventValues = new ContentValues();
    eventValues.put(Events.TITLE, "title");
    eventValues.put(Events.EVENT_LOCATION, "location");
    eventValues.put(Events.DTSTART, startTimeMilliseconds);
    eventValues.put(Events.DTEND, endTimeMilliseconds);
    eventValues.put(Events.CALENDAR_ID, "1");//Defaul calendar
    eventValues.put(Events.EVENT_TIMEZONE, TimeZone.SHORT);
    cr.insert(Events.CONTENT_URI, eventValues);
}

But CalendarView is not made to be used with events. CalendarView不能与事件一起使用。 In Android documentation you can read this for CalendarView : 在Android文档中,您可以阅读CalendarView

"This class is a calendar widget for displaying and selecting dates". “此类是用于显示和选择日期的日历小部件”。

I couldn't find an easy way to change cell property. 我找不到改变细胞特性的简单方法。
If you want to add event in your own caledar is better to implement your own calendar where you can change all views' properties. 如果您想在自己的caledar中添加事件,最好实现自己的日历,您可以在其中更改所有视图的属性。 There is plenty of calendar implementations you can find in google.com 您可以在google.com上找到大量日历实施

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM