捕获TimePicker的取消或关闭

时间:2012-05-12 15:29:48

标签: android

我不确定如何在下面的代码中实现onCancel / onDismiss侦听器。有人可以帮忙。

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;
    }

编辑:工作代码如下。点击设置就可以了。除了复选框之外的任何其他内容。

        @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;
    }

1 个答案:

答案 0 :(得分:2)

不要像TimePickerDialog方法那样直接返回onCreateDialog,而应该实例化它并添加onCancel侦听器。

...
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;
...