TimePicker Dialog from clicking EditText

前端 未结 9 1159
北海茫月
北海茫月 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 20:58

    Why not write in a re-usable way ?

    Create SetTime class:

    class SetTime implements OnFocusChangeListener, OnTimeSetListener {   
    
           private EditText editText;
           private Calendar myCalendar;
    
           public SetTime(EditText editText, Context ctx){
               this.editText = editText;
               this.editText.setOnFocusChangeListener(this);
               this.myCalendar = Calendar.getInstance();
    
           }
    
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if(hasFocus){
                    int hour = myCalendar.get(Calendar.HOUR_OF_DAY);
                    int minute = myCalendar.get(Calendar.MINUTE);
                    new TimePickerDialog(ctx, this, hour, minute, true).show();
                }
            }
    
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                // TODO Auto-generated method stub
                this.editText.setText( hourOfDay + ":" + minute);
            }
    
        }
    

    Then call it from onCreate function:

        EditText editTextFromTime = (EditText) findViewById(R.id.editTextFromTime);
        SetTime fromTime = new SetTime(editTextFromTime, this);
    
    0 讨论(0)
  • 2020-11-30 20:59

    For me the dialogue appears more than one if I click the dpFlightDate edit text more than one time same for the timmer dialog . how can I avoid this dialog to appear only once and if the user click's 2nd time the dialog must not appear again ie if dialog is on the screen ?

              // perform click event on edit text
                dpFlightDate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // calender class's instance and get current date , month and year from calender
                        final Calendar c = Calendar.getInstance();
                        int mYear = c.get(Calendar.YEAR); // current year
                        int mMonth = c.get(Calendar.MONTH); // current month
                        int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
                        // date picker dialog
                        datePickerDialog = new DatePickerDialog(frmFlightDetails.this,
                                new DatePickerDialog.OnDateSetListener() {
                                    @Override
                                    public void onDateSet(DatePicker view, int year,
                                                          int monthOfYear, int dayOfMonth) {
                                        // set day of month , month and year value in the edit text
                                        dpFlightDate.setText(dayOfMonth + "/"
                                                + (monthOfYear + 1) + "/" + year);
    
                                    }
                                }, mYear, mMonth, mDay);
                        datePickerDialog.show();
                    }
                });
                tpFlightTime.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // Use the current time as the default values for the picker
                        final Calendar c = Calendar.getInstance();
                        int hour = c.get(Calendar.HOUR_OF_DAY);
                        int minute = c.get(Calendar.MINUTE);
                        // Create a new instance of TimePickerDialog
                        timePickerDialog = new TimePickerDialog(frmFlightDetails.this, new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                                tpFlightTime.setText( selectedHour + ":" + selectedMinute);
                            }
                        }, hour, minute, true);//Yes 24 hour time
                        timePickerDialog.setTitle("Select Time");
                        timePickerDialog.show();
                    }
                });
    
    0 讨论(0)
  • 2020-11-30 21:01
    public class **Your java Class** extends ActionBarActivity implements  View.OnClickListener{
    date = (EditText) findViewById(R.id.date);
    date.setInputType(InputType.TYPE_NULL);
            date.requestFocus();
            date.setOnClickListener(this);
            dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    
            setDateTimeField();
    
    private void setDateTimeField() {
            Calendar newCalendar = Calendar.getInstance();
            fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
    
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Calendar newDate = Calendar.getInstance();
                    newDate.set(year, monthOfYear, dayOfMonth);
                    date.setText(dateFormatter.format(newDate.getTime()));
                }
    
            }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    
        }
    @Override
        public void onClick(View v) {
            fromDatePickerDialog.show();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 21:06
    eReminderTime.setText( "" + selectedHour + ":" + selectedMinute);
    

    Your missing a + between "" and selected hour, setText methods only take a single string, so you need to concatenate all the parts (the first quotes are likely unnecessary).

    eReminderTime.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Calendar mcurrentTime = Calendar.getInstance();
                int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                int minute = mcurrentTime.get(Calendar.MINUTE);
                TimePickerDialog mTimePicker;
                mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                        eReminderTime.setText( selectedHour + ":" + selectedMinute);
                    }
                }, hour, minute, true);//Yes 24 hour time
                mTimePicker.setTitle("Select Time");
                mTimePicker.show();
    
            }
        });
    

    That should fix your second error, you weren't providing the last parameter. TimePickerDialog Constructors

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 21:10

    You can use the below code in the onclick listener of edittext

      TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
        new TimePickerDialog.OnTimeSetListener() {
    
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay,
                                  int minute) {
    
                tv_time.setText(hourOfDay + ":" + minute);
            }
        }, hour, minute, false);
         timePickerDialog.show();
    

    You can see the full code at Android timepicker example

    0 讨论(0)
提交回复
热议问题