意外解除了Android中的TimePickerDialog

时间:2014-04-16 13:52:20

标签: android

在我的Android应用程序中,我有一个TimePickerDialog活动。当我在对话框外触摸屏幕时,我的TimePickerDialog会解散。我可以避免这个吗?

这是我的代码:

protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            // set time picker as current time
            Calendar c = Calendar.getInstance();
            int currentHour = c.get(Calendar.HOUR_OF_DAY);
            int currentMinute = c.get(Calendar.MINUTE);
            TimePickerDialog timePickerDialog = new TimePickerDialog(this, 
                    timePickerListener, currentHour, currentMinute,true);
            timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                   if (which == DialogInterface.BUTTON_NEGATIVE) {
                       startPreviousActivity();
                   }
                }
              });
            timePickerDialog.setOnCancelListener(new OnCancelListener(){
                 @Override
                 public void onCancel(DialogInterface dialog){
                    startPreviousActivity();
              }});  
            timePickerDialog.setTitle(timeSetting);
            Window window = timePickerDialog.getWindow();
            window.setGravity(Gravity.TOP);
            WindowManager.LayoutParams wlp = window.getAttributes();
            wlp.y = (int)(100 * dpHeight / MIN_HEIGHT);
            window.setAttributes(wlp);
            return timePickerDialog;
        }
        return null;
    }

1 个答案:

答案 0 :(得分:1)

您可以使用Dialog的方法setCanceledOnTouchOutside来执行此操作。

timePickerDialog.setCanceledOnTouchOutside(false);