3

我不确定如何为下面的代码实现 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;
    }
4

1 回答 1

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;
...
于 2012-05-12T15:50:30.157 回答