TimePickerDialog cancel button

前端 未结 6 1176
暗喜
暗喜 2021-01-06 07:30

I have an activity - TimePickerActivity - which creates a TimePickerDialog. I have a onTimeSetListener which responds to the Set button at the end of which it calls finish(

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-06 07:54

    Just pass an onCancelListener to TimePicker.setOnCancelListener()

    edit: After Ron's problems with implementation I decided to actually test myself the code (I answered just looking at the API) and I discovered than even if the code is correct (I suppose you had a typo somewhere as my code compiles ok), when clicking the cancel button it didn't respond as intended...

    It appears that when you click cancel button the Dialog doesn't call the cancel() method that fires the OnCancelListener as it would seem the obvious, but the dismiss() method that fires an OnDismissListener, pretty weird...

    So this code is working fine for me:

    TimePickerDialog.OnTimeSetListener mTimeSetListener = new OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            //time set stuff
        }
    };
    TimePickerDialog myTPDialog = new TimePickerDialog(this,mTimeSetListener,0,0,false);
    myTPDialog.setOnDismissListener(new OnDismissListener() {
        public void onDismiss(DialogInterface dialog) {
            // Cancel code here
        }
    });
    myTPDialog.show();
    

    all credits to this SO answer...

提交回复
热议问题