2012-08-17 145 views
2

我有以下代码来按下按钮时执行某些操作。我希望能够在2013年3月3日上午10点按钮创建日历活动。所有的帮助表示赞赏。在默认的安卓日历中创建日历事件

代码:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Button button = (Button)findViewById(R.id.button1); 
    // button.setOnClickListener(this); 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1); 
    final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2); 


    final Button button = (Button) findViewById(R.id.button1); 
    button.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) { 


      if (checkBox.isChecked()) { 

回答

3

你可以用意向的帮助下打开日历。

以下是在Calendar应用程序中设置事件的代码。您只能打开默认事件字段填充的日历活动。

Intent intent = new Intent(Intent.ACTION_EDIT); 
intent.setType("vnd.android.cursor.item/event"); 
intent.putExtra("title", "Some title"); 
intent.putExtra("description", "Some description"); 
intent.putExtra("beginTime", eventStartInMillis); 
intent.putExtra("endTime", eventEndInMillis); 
startActivity(intent); 

将上面的代码放在按钮的onclick监听器中。

此外,您必须在manifest.xml中添加这些日历权限:

android:name="android.permission.READ_CALENDAR" 
android:name="android.permission.WRITE_CALENDAR" 
+0

好的,那么如何在同一个按钮下添加另一个事件?我想要生成多个事件。 – ScorpioBlue 2012-08-17 04:03:47

+0

用上面的代码做一个函数,并且如果你想添加N个事件,则调用该函数的N个时间。 – rajpara 2012-08-17 04:26:18

+0

丹J&Klaudo感谢编辑:) – rajpara 2012-08-17 05:48:50

2

Android的编码器提供了一种简单的方法来完成这个任务

此外,您必须在清单中添加权限.XML使用日历事件

机器人:名字= “android.permission.READ_CALENDAR”

机器人:名字=“android.permission.WRITE_CA LENDAR“

+0

感谢您指导我的缺点,如果您编辑我的答案,我会很高兴。 – rajpara 2012-08-17 04:25:08

+0

+1为你指导我。 – rajpara 2012-08-17 05:49:09