在Android calendarView小部件上显示Google日历? [英] Display google calendar on Android calendarView widget?

查看:84
本文介绍了在Android calendarView小部件上显示Google日历?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,有没有一种方法可以在应用程序内部使用Android calendarView显示Google日历?我找不到办法

My question is simple, is there a way to display a Google Calendar using Android calendarView inside an application? I can't find a way to do so

推荐答案

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Introducing ICS Calendar"
android:gravity="center"
 android:padding="10dip"/>
<TextView
android:id="@+id/data"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:padding="10dip"/>
<LinearLayout
android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/previous"
 android:text="Prev"
 android:padding="10dip"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next"
 android:text="Next"
 android:padding="10dip"/>
</LinearLayout>
</LinearLayout>

Main.java

Main.java

import java.text.Format;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener{

private Cursor mCursor = null;
private static final String[] COLS = new String[]
{ CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART};
}

现在,我们需要重写on create方法.要特别注意我们如何填充数据库游标.这是我们需要先前定义的COLS常数的地方.您还将注意到,在初始化数据库游标并设置了点击处理程序回调之后,我们将继续手动调用on click处理程序.此快捷方式使我们可以在不重复代码的情况下初始填充UI.

Now we need to override the on create method. Pay special attention to how we populate the database cursor. This is where we need our previously defined COLS constant. You’ll note also that after the database cursor is initialized and the click handler callbacks are set, we go ahead and manually invoke the on click handler. This shortcut allows us to initially fill out our UI without having to repeat code.

Main.java

Main.java

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCursor = getContentResolver().query(
CalendarContract.Events.CONTENT_URI, COLS, null, null, null);
mCursor.moveToFirst();
Button b = (Button)findViewById(R.id.next);
b.setOnClickListener(this);
b = (Button)findViewById(R.id.previous);
b.setOnClickListener(this);
onClick(findViewById(R.id.previous));
}

在回调中,我们将把光标操纵到数据库中的正确条目并更新UI.

In our callback, we will manipulate the cursor to the correct entry in the database and update the UI.

@Override
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.data);
String title = "N/A";
Long start = 0L;
switch(v.getId()) {
case R.id.next:
if(!mCursor.isLast()) mCursor.moveToNext();
break;
case R.id.previous:
if(!mCursor.isFirst()) mCursor.moveToPrevious();
break;
}
Format df = DateFormat.getDateFormat(this);
Format tf = DateFormat.getTimeFormat(this);
try {
title = mCursor.getString(0);
start = mCursor.getLong(1);
} catch (Exception e) {
//ignore
}
tv.setText(title+" on "+df.format(start)+" at "+tf.format(start));
}

权限:

<uses-permission android:name="android.permission.READ_CALENDAR"/>

这篇关于在Android calendarView小部件上显示Google日历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆