TimePickerDialog.onTimeSetListener not called

前端 未结 1 661
孤街浪徒
孤街浪徒 2020-12-19 06:06

I\'m using the following code to display a TimePickerDialog:

TimePickerDialog dialog = new TimePickerDialog(this, new OnTimeSetListener() {

            


        
相关标签:
1条回答
  • 2020-12-19 06:34

    TimePickerDialog has changed after Android 4.1.1 and there is a bug about cancellation of the both TimePickerDialog and DatePickerDialog. Please read first here. By default you do not need to set positive and negative button. TimePickerDialog and DatePickerDialog handles these for you. So if this cancellation issue is not important for you delete setting of positive and negative button. If you delete those in both version if user clicks OK button your onTimeSet method will be called.

    But I recommend until that bug will be fixed use custom made AlertDialog with TimePicker widget;

        final TimePicker timePicker = new TimePicker(this);
        timePicker.setIs24HourView(true);
        timePicker.setCurrentHour(20);
        timePicker.setCurrentMinute(15);
    
        new AlertDialog.Builder(this)
                .setTitle("Test")
                .setPositiveButton(android.R.string.ok, new OnClickListener() {
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("Picker", timePicker.getCurrentHour() + ":"
                                + timePicker.getCurrentMinute());
                    }
                })
                .setNegativeButton(android.R.string.cancel,
                        new OnClickListener() {
    
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                Log.d("Picker", "Cancelled!");
                            }
                        }).setView(timePicker).show();
    
    0 讨论(0)
提交回复
热议问题