简体   繁体   中英

Catching cancel or dismiss for TimePicker

I'm not sure how to implement an onCancel/onDismiss listener to the code below. Could somebody please assist.

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    mHour = hourOfDay;
                    mMinute = minute;
                    time = ""+mHour+":"+mMinute;
                    notifications();
                }
    };



@Override
   protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, mHour, mMinute, false);
        }
        return null;
    }

Edit: Working code is below. When you click set it does it's thing. Anything else it removed the checkbox.

        @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            TimePickerDialog myTime = new TimePickerDialog(this,mTimeSetListener, mHour, mMinute, false);
            myTime.setOnCancelListener(new OnCancelListener(){
                CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
                public void onCancel(DialogInterface dialog) {
                    Log.d("IN HERE","HERE2");
                    cbChecked = false;
                    notValue = 0;
                    checkBox.setChecked(false);
                    checkBox.setText("Use reminders");
                }
            });
            return myTime;
        }
        return null;
    }

Instead of returning the TimePickerDialog directly like you are on the onCreateDialog method, you should instantiate it and add a onCancel listener.

...
case TIME_DIALOG_ID:
   TimePickerDialog myTime = new TimePickerDialog(this,yourListener, h, m, false);
   myTime(new DialogInterface.OnCancelListener(){
      @Override
      public void onCancel(DialogInterface arg0) {
        //your stuff
      }
   );//For cancel button

   return myTime; //Return the dialog
break;
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM