CalendarView android中的setOnClickListener

时间:2013-10-25 09:17:34

标签: android android-fragments

我需要在setOnClickListener中实施CalendarView,但它不起作用,因为CalendarViewsubview或类似的东西。我试过了:

calendarView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

        }
    });

但它不起作用

请有人知道我该如何实施它?

由于

1 个答案:

答案 0 :(得分:1)

    private int mYear;
    private int mMonth;
    private int mDay;    
    static final int DATE_PICKER_ID = 1111;
final Calendar c = Calendar.getInstance();
       mYear  = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay   = c.get(Calendar.DAY_OF_MONTH);


calenderView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                showDialog(DATE_PICKER_ID);
            }
        });     


@Override
    protected Dialog onCreateDialog(int id) {
       switch (id) {

  case DATE_PICKER_ID:

// set date picker as current date

   return new DatePickerDialog(this, pickerListener , mYear, mMonth,mDay );

}

return null;

    }

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

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            mDay = selectedDay;
            mMonth = selectedMonth;
            mYear = selectedYear;
            Calendar c = Calendar.getInstance();
            c.set(mYear, mMonth, mDay);
            SimpleDateFormat df = new SimpleDateFormat("MMM-dd-yyyy");
            String selectedDate = df.format(c.getTime());
            calenderView.setText(selectedDate);         
           }
        };