2013-04-24 51 views
0

我被困了2天,最后放弃。我需要你的日历功能帮助。该人员选择一个产品(在按钮上),并设置一条消息,并允许他们在可以重新订购时添加提醒。现在我打开日历,但无法让日历根据该产品设置日期。我不确定如何设置字符串值,调用它,并设置日历日期。我知道这是基本的东西,但我很感激帮助!对不起,冗长的代码。我想确保我包含了所有相关的东西。使用字符串值设置日历订单日期

这里是我的代码:

Button reorder1 = (Button) findViewById(R.id.rotramadol); 
Button reorder2 = (Button) findViewById(R.id.roaciphex); 
Button reorder3 = (Button) findViewById(R.id.roflexeril); 
reorder1.setOnClickListener(this); 
reorder2.setOnClickListener(this); 
reorder3.setOnClickListener(this); 

@Override 
public void onClick(View v) { 
    if (v.getId() == R.id.rotramadol) { 
//this one needs the calendar set 10 days from now 
     AlertDialog ad = new AlertDialog.Builder(this) 
       .setMessage(
         "Order Quantity 90 tabs requires 10 days") 
       .setTitle("Tramadol") 
       .setPositiveButton("Set Reminder", this) 
       .setNegativeButton("OK", this).setCancelable(false) 
       .create(); 
     ad.show(); 
    } else if (v.getId() == R.id.roaciphex) { 
//this one needs the calendar set 25 days from now 
     AlertDialog ad1 = new AlertDialog.Builder(this) 
       .setMessage("Order Quantity 30 tabs requires 25 days") 
       .setTitle("Aciphex") 
       .setPositiveButton("Set Reminder", this) 
       .setNegativeButton("OK", this).setCancelable(false) 
       .create(); 
     ad1.show(); 
    } else if (v.getId() == R.id.roflexeril) { 
//this one needs the calendar set 7 days from now 
     AlertDialog ad2 = new AlertDialog.Builder(this) 
       .setMessage("Order Quantity 30 tabs requires 7 days") 
       .setTitle("Flexeril") 
       .setPositiveButton("Set Reminder", this) 
       .setNegativeButton("OK", this).setCancelable(false) 
       .create(); 
     ad2.show(); 


@Override 
public void onClick(DialogInterface dialog, int which) { 
    // TODO Auto-generated method stub 

    switch (which) { 
    case DialogInterface.BUTTON_POSITIVE: // yes 

     Intent intent = new Intent(Intent.ACTION_INSERT); 
     intent.setType("vnd.android.cursor.item/event"); 
     intent.putExtra(Events.TITLE, "Reorder"); 
     intent.putExtra(Events.EVENT_LOCATION, ""); 
     intent.putExtra(Events.DESCRIPTION, 
       "https://www.place they reorder.com"); 

     GregorianCalendar calDate = new GregorianCalendar(); 
     intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, 
       calDate.getTimeInMillis()); 
     intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, 
       calDate.getTimeInMillis()); 

     intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true); 

     intent.putExtra(Events.RRULE, "FREQ=DAILY;COUNT=1;"); 

     intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE); 
     intent.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY); 

     startActivity(intent); 

     break; 

    case DialogInterface.BUTTON_NEGATIVE: // no 

     dialog.cancel(); 

     break; 
    default: 
     break; 

    } 

} 

回答

0

我假设你正在试图建立基于用户选择的日历事件,尝试下面的代码

    Calendar calendar = Calendar.getInstance(); 
        calendar.add(Calendar.DATE, 5/*how many days from today*/); 
        Intent intent = new Intent(Intent.ACTION_EDIT); 
        intent.setType("vnd.android.cursor.item/event"); 
        intent.putExtra(Events.TITLE, "your title"); 
        intent.putExtra("beginTime", calendar.getTime()); 
        intent.putExtra("endTime", calendar.getTime() + DateUtils.MINUTE_IN_MILLIS * 30); 
        intent.putExtra(Events.EVENT_TIMEZONE, calendar.getTimeZone()); 
        startActivity(intent); 
+0

感谢您的答复。添加日期(在您的示例中为5天)需要基于他们选择哪个按钮。它随每个按钮而改变。你能帮我吗?我猜数字5会改为每个按钮上设置的字符串值? – JeffK 2013-04-24 20:22:09

+0

您可以将任意数量的日期添加到日历中。如果您想在10天后设置提醒,请将值传递为10.尝试使用某种逻辑来确定用户点击了哪个按钮。 – deepdroid 2013-04-24 20:27:07

+0

do'parseInt(String)'如果你想从字符串中创建5个变量。 – StoneBird 2013-04-24 20:33:44