Android Date Time picker in one dialog

后端 未结 9 1194
抹茶落季
抹茶落季 2021-02-07 01:31

I am using material Date Time picker for my Android app. But I want to combine the Date and Time picker in one dialog.

<
9条回答
  •  名媛妹妹
    2021-02-07 01:58

    You can call TimePicker after DatePicker set Date. Like this..

        String date_time = "";
        int mYear;
        int mMonth;
        int mDay;
    
        int mHour;
        int mMinute;
    
        et_show_date_time = (EditText) findViewById(R.id.et_show_date_time);
        btn_set_date_time = (Button) findViewById(R.id.btn_set_date_time);
    
        btn_set_date_time.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                datePicker();
    
            }
        });
    

    Date Picker:

        private void datePicker(){
    
        // Get Current Date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
    
        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
    
                    @Override
                    public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
    
                        date_time = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
                       //*************Call Time Picker Here ********************
                       tiemPicker();
                    }
                }, mYear, mMonth, mDay);
           datePickerDialog.show();
        }
    

    Time Picker

     private void tiemPicker(){
        // Get Current Time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);
    
        // Launch Time Picker Dialog
        TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {
    
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
    
                        mHour = hourOfDay;
                        mMinute = minute;
    
                        et_show_date_time.setText(date_time+" "+hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        timePickerDialog.show();
    }
    

    --

提交回复
热议问题