Android: DatePicker and DatePicker Dialog

前端 未结 8 569
梦如初夏
梦如初夏 2020-12-24 09:23

I have this code here on the options menu

Dialog dialog = new Dialog(ScheduleActivity.this);
dialog.setTitle(\"Add Event\");
dialog.setContentView(R.layout.         


        
8条回答
  •  太阳男子
    2020-12-24 09:51

    This is my helper function which get context and TextView as parameter and sets Date on that text view when user select a date:

            public static void showDate(final Context context, final TextView textView) {
    
        if (textView != null) {
            final Calendar myCalendar = Calendar.getInstance();
            DatePickerDialog.OnDateSetListener 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);
                    String myFormat = "MM/dd/yy"; // In which you need put here
                    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                    //UIHelper.showLongToastInCenter(context, sdf.format(myCalendar.getTime()));
                    textView.setText(sdf.format(myCalendar.getTime()));
                }
    
            };
            new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        } else {
            UIHelper.showLongToastInCenter(context, "Unable to show Date picker");
        }
    }
    

提交回复
热议问题