TimePicker Dialog from clicking EditText

前端 未结 9 1209
北海茫月
北海茫月 2020-11-30 20:16

I\'ve already got a DatePicker which pops up when the user clicks on the EditText field

eReminderDate.setOnClickListener(new OnClickListener() {

                    


        
9条回答
  •  盖世英雄少女心
    2020-11-30 21:07

    I dont know if this will work for you, it works for me just fine.

    Create a method for the Date/Time picker dialog.

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    
        // when dialog box is called, below method will be called.
        // The arguments will be working to get the Day of Week to show it in a special TextView for it.
    
        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {
            String year1 = String.valueOf(selectedYear);
            String month1 = String.valueOf(selectedMonth + 1);
            String day1 = String.valueOf(selectedDay);
            delivDate.setText(month1 + "/" + day1 + "/" + year1);
            delivDay.setText(DateFormat.format("EEEE", new Date(selectedYear, selectedMonth, selectedDay - 1)).toString());
        }
    };
    

    and then, wherever you want you can do it just like this

    public void setDateOnClick (View view) {
        Calendar cal = Calendar.getInstance();
    
        DatePickerDialog datePicker = new DatePickerDialog(this, datePickerListener,
                cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH));
        //Create a cancel button and set the title of the dialog.
        datePicker.setCancelable(false);
        datePicker.setTitle("Select the date");
        datePicker.show();
    }
    

    hope you find this as your solution.

提交回复
热议问题