开发者

Displaying all the days of a month

In my application, when the user selects to view the details of the current month, a new screen will open. In that screen, we will have a drop down menu. In the drop down menu I want to display all the dates of that month. So that h开发者_Python百科e can select the date he is interested in.

For example, if the current month is Feb, then drop down menu is expected to contain 28 numbers( sometimes 29).

I have used xml layout in my app.

Could anyone suggest me a better way of implementing this...?


Try using DatePicker...

here is the sample code..

Write this in your activity class..

static final int DATE_DIALOG_ID = 0;

@Override
    protected Dialog onCreateDialog(int id) {
        Calendar c = Calendar.getInstance();
        int cyear = c.get(Calendar.YEAR);
        int cmonth = c.get(Calendar.MONTH);
        int cday = c.get(Calendar.DAY_OF_MONTH);
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, mDateSetListener, cyear, cmonth,
                    cday);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        // onDateSet method
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) { 
            String date_selected = String.valueOf(dayOfMonth) + " - "
                    + String.valueOf(monthOfYear + 1) + " - "
                    + String.valueOf(year);
             Toast.makeText(getApplicationContext(), "Selected Date is =" +   date_selected, Toast.LENGTH_SHORT).show();

        }
    };

and call this whenever you want to display the Datepicker..

showDialog(DATE_DIALOG_ID);

That displays a nice DatePicker from which you can select a date....


Use DatePicker widget, here is the example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜