1

任何人都可以通过单击一个简单的按钮来帮助我如何在 android 中显示日历

我在 andriod 中有一个按钮,如果我们单击一个按钮,用户应该能够从日历中选择日期

4

3 回答 3

4

您可以为此使用 DatePickerDialog 我正在为您发布示例代码

static final int DATE_DIALOG_ID = 999; 
DatePickerDialog.OnDateSetListener date;

date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel(val);
            }
        };


        depart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(DATE_DIALOG_ID);
                val = 1;
            }
        });


        protected Dialog onCreateDialog(int id) {
        final Calendar now = Calendar.getInstance();

        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            DatePickerDialog _date = new DatePickerDialog(this, date,
                    myCalendar.get(Calendar.YEAR),
                    myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)) {
                @Override
                public void onDateChanged(DatePicker view, int year,
                        int monthOfYear, int dayOfMonth) {
                    if (year - now.get(Calendar.YEAR) > 1) {
                        view.updateDate(year - 1,
                                myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));
                    }
                    if (year - now.get(Calendar.YEAR) == 1
                            && monthOfYear > now.get(Calendar.MONTH)) {
                        view.updateDate(year, myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));
                    }
                    if (year - now.get(Calendar.YEAR) == 1
                            && monthOfYear == now.get(Calendar.MONTH)
                            && dayOfMonth > now.get(Calendar.DAY_OF_MONTH)) {
                        view.updateDate(year, myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));
                    }
                    if (year < now.get(Calendar.YEAR))

                        view.updateDate(myCalendar.get(Calendar.YEAR),
                                myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));

                    if (monthOfYear < now.get(Calendar.MONTH)
                            && year == now.get(Calendar.YEAR))
                        view.updateDate(myCalendar.get(Calendar.YEAR),
                                myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));

                    if (dayOfMonth < now.get(Calendar.DAY_OF_MONTH)
                            && year == now.get(Calendar.YEAR)
                            && monthOfYear == now.get(Calendar.MONTH))
                        view.updateDate(myCalendar.get(Calendar.YEAR),
                                myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));
                }
            };
            return _date;
        }
        return null;
    }

设置日期:

private void updateLabel(int val) {
        String myFormat = "dd/MM/yy"; // In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        Log.d("Date value ", "===" + sdf.format(myCalendar.getTime()));
        depart.setText(sdf.format(myCalendar.getTime()));


    }
于 2013-09-30T06:22:02.727 回答
1

要从按钮单击事件打开日历,请将此代码添加到 onClick 方法:

 Intent i = new Intent();

//Froyo or greater (mind you I just tested this on CM7 and the less than froyo one worked so it depends on the phone...)
cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");

//less than Froyo
cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");

i.setComponent(cn);
startActivity(i);
于 2013-09-30T06:02:25.440 回答
0

如果您的意思是日期选择器,那么您可以检查一下

或者您可以将此库用作最新版本的日期选择器。

于 2013-09-30T05:59:54.403 回答