2

解决方案还没有找到,我已经花了几个小时来应对这个挑战。当你按下“今天”按钮时,我怎样才能得到一个日期?

想要获得按下“今天”按钮时显示的日期。并且,我们相信试图将信息添加到指定日期的屏幕显示的过程。

    DatePickerDialog dpdialog = new DatePickerDialog(me, new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int pickyear, int monthOfYear,
                int dayOfMonth) {

        }
    }, year, month-1, day);
    // Dialog(Positive Button)
    dpdialog.setButton(
            DialogInterface.BUTTON_POSITIVE,
            "set", 
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Positive Button
                    // How to get the date set
                    String day = dpdialog.getDayOfMonth();
                    String month = dpdialog.getMonth() + 1;
                    String year = dpdialog.getYear();
                }
            }    
            );

    // Dialog (Negative Button)
    dpdialog.setButton(
            DialogInterface.BUTTON_NEGATIVE, 
            "cancel", 
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Negative Button
                }
            }
            );

    // Dialog(Neutral Button)
    dpdialog.setButton(
            DialogInterface.BUTTON_NEUTRAL, 
            "today", 
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Neutral Button
                }
            }
            );

    dpdialog.show();
4

2 回答 2

1

外部类中定义的变量将从内部类/内联方法中可见,因此您可以这样做:

private String day;
private String month;
private String year;

DatePickerDialog dpdialog = new DatePickerDialog(me, new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int pickyear, int monthOfYear, int dayOfMonth) {
        // you have the picked year/month/day here.
        // For example:
        day = String.valueOf(dayOfMonth);
        month = String.valueOf(monthOfYear);
        year = String.valueOf(pickyear);
    }

}, year, month-1, day);
于 2012-03-13T03:10:51.530 回答
1
    //decleration
 final static int DATE_DIALOG_ID=1;
EditText dateedt;
String date_selected="";
int currentDay,currentYear,currentMonth,

//onCreate
Calendar calendar=Calendar.getInstance();
        currentDay=calendar.get(calendar.DAY_OF_MONTH);
        currentYear=calendar.get(calendar.YEAR);
        currentMonth=calendar.get(calendar.MONTH)+1;

dateedt=(EditText)findViewById(R.id.dateedt);

dateedt.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(DATE_DIALOG_ID);
            }
        });
//methods
@Override
        protected Dialog onCreateDialog(int id) {
            // TODO Auto-generated method stub

            switch(id)
            {
            case DATE_DIALOG_ID:
                return new DatePickerDialog( this,mDateSetListener,currentYear,currentMonth-1,currentDay);
}
            return null;
        }

DatePickerDialog.OnDateSetListener mDateSetListener=new OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub

                if(monthOfYear < 10 && !String.valueOf(monthOfYear).contains("0"))
                {
                    if(dayOfMonth < 10 && !String.valueOf(dayOfMonth).contains("0"))
                    {
                        date_selected = "0"+String.valueOf(monthOfYear+1)+" -0"+String.valueOf(dayOfMonth)+" -"+String.valueOf(year);
                        //selectedDateForDb = String.valueOf(year)+"-"+"0"+String.valueOf(monthOfYear+1)+"-0"+String.valueOf(dayOfMonth);
                    }
                    else
                    {
                        date_selected = "0"+String.valueOf(monthOfYear+1)+" -"+String.valueOf(dayOfMonth)+" -"+String.valueOf(year);
                        //selectedDateForDb = String.valueOf(year)+"-0"+String.valueOf(monthOfYear+1)+"-"+String.valueOf(dayOfMonth);
                    }
                }
                else if(dayOfMonth < 10 && !String.valueOf(dayOfMonth).contains("0"))
                {
                    date_selected = String.valueOf(monthOfYear+1)+" -0"+String.valueOf(dayOfMonth)+" -"+String.valueOf(year);
                    //selectedDateForDb = String.valueOf(year)+"-"+String.valueOf(monthOfYear+1)+"-0"+String.valueOf(dayOfMonth);
                }
                else
                {
                    date_selected = String.valueOf(monthOfYear+1)+" -"+String.valueOf(dayOfMonth)+" -"+String.valueOf(year);
                    //selectedDateForDb = String.valueOf(year)+"-"+String.valueOf(monthOfYear+1)+"-"+String.valueOf(dayOfMonth);

                }
                dateedt.setText(date_selected);
            }
        };
于 2012-03-13T03:30:11.377 回答