TimePickerDialog自动关闭set

时间:2015-01-29 10:49:47

标签: android view timepicker dismiss

如何禁用TimePickerDialog以在按下设置时关闭?

我需要检查价值是否良好,如果不是,我不希望它被解雇。

mTimePicker.setCancelable(true);

但我确实希望用户能够取消取消...

    TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker,
                                int selectedHour, int selectedMinute) {

                            if(condition){
                            mTimePicker.dismiss();

                               }else{
                            showMessage();
                                    }

                        }
                    }, hour, minute, false);// Yes 24 hour time
            mTimePicker.setTitle(getString(R.string.select_time));

            mTimePicker.show();

2 个答案:

答案 0 :(得分:1)

我将删除Jesus Dimrix的“唯一解决方案”的示例代码,以再次调用mTimepicker.show()。

Calendar c = Calendar.getInstance();
final int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
       @Override
       public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
           if(hourOfDay > 12){
               timePickerDialog = new TimePickerDialog(MainActivity.this,this,hourOfDay,minute,true);
               timePickerDialog.show();
               Toast.makeText(MainActivity.this,"Must be less than 12",Toast.LENGTH_SHORT).show();
           }else{
               Toast.makeText(MainActivity.this,hourOfDay + ":" + minute,Toast.LENGTH_SHORT).show();
                }
            }
       }, hour, minute, true);
       timePickerDialog.show();

我希望其他人可以提供更好的解决方案。

答案 1 :(得分:0)

使用任何一个:

mTimePicker.cancel();

mTimePicker.dismiss();