Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

android - TimePickerDialog.onTimeSetListener not called

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

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

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            System.out.println("onTimeSet");
        }
    }, 20, 15, true);
    dialog.setTitle("Test");
    dialog.setButton(TimePickerDialog.BUTTON_POSITIVE, "Done", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            System.out.println("Done");
        }
    });
    dialog.setButton(TimePickerDialog.BUTTON_NEGATIVE, "Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            System.out.println("Cancel");
        }
    });
    dialog.show();

On a device running Android 2.3.7, the onTimeSet method doesn't get called (the onClick methods do). On a device running 4.2.2 the onTimeSet does get called like it should.

What is happening here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...