作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 Android 4.0 默认日历中,如果我们想移动到下个月,则需要滚动日历,但我需要通过按上图中存在的箭头移动到下个月或上个月。而且我还想添加到默认日历。如果添加事件,则该日期应像图像中那样标记。
我使用了这个 CalendarView。
<CalendarView
android:id="@+id/calendarView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
我想知道如何实用地将事件添加到此 CalendarView。
最佳答案
要移动到下个月或上个月,您可以使用此代码:
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);
}
});
如果您想在默认日历中添加一个事件,您必须查看@adam2510 发布的链接。这是您可以使用的示例代码:
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);
}
但是 CalendarView
并不是为了与事件一起使用。在 Android 文档中,您可以为 CalendarView
阅读此内容:
"This class is a calendar widget for displaying and selecting dates".
我找不到更改单元格属性的简单方法。
如果您想在自己的日历中添加事件,最好实现自己的日历,您可以在其中更改所有 View 的属性。您可以在 google.com 中找到大量日历实现
关于android - 如何在Android 4.0 默认日历中添加事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12152653/
我是一名优秀的程序员,十分优秀!