如何在用户接受详细信息的同时在日历中添加事件

时间:2013-08-03 06:49:16

标签: android android-calendar

我接受用户的详细信息喜欢:活动名称,日期和时间

现在我想在日历中添加此事件

向我展示如何将事件保存到日历,方法是点击保存到日历按钮

请参阅下面的来源

活动代码:

public class MainActivity extends Activity {

int hour, minute, mYear,mMonth, mDay;

static final int TIME_DIALOG_ID = 0;
static final int DATE_DIALOG_ID = 1;

private EditText txtName;
private EditText txtDate;    
private EditText txtTime;
private String[] arrMonth = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

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

    txtName = (EditText) findViewById(R.id.edit_event_name);
    txtDate = (EditText) findViewById(R.id.edit_event_date);
    txtTime = (EditText) findViewById(R.id.edit_event_time);


     final Button button = (Button) findViewById(R.id.btn_savetocalendar);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click

             String name = txtName.getText().toString();
             Toast.makeText(getApplicationContext(), name.toString(), Toast.LENGTH_LONG).show();
         }
     });


    // get the current date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    txtDate.setOnTouchListener(new OnTouchListener() {
        @SuppressWarnings("deprecation")
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG_ID);
            return true;
        }
    });

    txtTime.setOnTouchListener(new OnTouchListener() {
        @SuppressWarnings("deprecation")
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            showDialog(TIME_DIALOG_ID);
            return true;
        }
    });

}

@Override
protected Dialog onCreateDialog(int id)
{
    switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(
                this, mTimeSetListener, hour, minute, true);
        case DATE_DIALOG_ID:
            return new DatePickerDialog(
                this, mDateSetListener, mYear, mMonth, mDay);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener()
    {

    public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        String sdate = arrMonth[mMonth] + " " + LPad(mDay + "", "0", 2) + ", " + mYear;
        txtDate.setText(sdate);
    }
};

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener()
{
    public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour)
    {
        hour = hourOfDay;
        minute = minuteOfHour;
        String stime = LPad(""+hour, "0", 2) + ":"+ LPad(""+hour, "0", 2);
        txtTime.setText(stime);
    }
};

private static String LPad(String schar, String spad, int len) {
    String sret = schar;
    for (int i = sret.length(); i < len; i++) {
        sret = spad + sret;
    }
    return new String(sret);
   }
}

0 个答案:

没有答案