android:TimePickerDialog阻止用户选择过去的时间,并可以选择具有新日期的未来时间

时间:2017-01-06 13:24:15

标签: java android android-studio android-fragments android-timepicker

我正在使用此链接Android TimePickerDialog set max time

我是android新手。借助此代码,我无法选择过去的时间,但我们无法选择未来的时间。在timepickerdialog模式中选择12时,根据第二天而不是过去一天自动更改为am。

3 个答案:

答案 0 :(得分:14)

试试这段代码:

TimePickerFragment timePickerFragment = new TimePickerFragment();
            timePickerFragment.setOnTimeSetListener(new OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    Calendar datetime = Calendar.getInstance();
                    Calendar c = Calendar.getInstance();
                    datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    datetime.set(Calendar.MINUTE, minute);
                    if (datetime.getTimeInMillis() >= c.getTimeInMillis()) {
                        //it's after current
                         int hour = hourOfDay % 12;
                    btnPickStartTime.setText(String.format("%02d:%02d %s", hour == 0 ? 12 : hour,
                            minute, hourOfDay < 12 ? "am" : "pm"));
                    } else {
                        //it's before current'
                        Toast.makeText(getApplicationContext(), "Invalid Time", Toast.LENGTH_LONG).show();
                    }
                }
            });
            timePickerFragment.show(getSupportFragmentManager(), "TIME");

它将显示timepicker对话框,如果用户在当前时间之前选择任何时间......它将显示无效时间..

修改 您需要导入:

import android.app.TimePickerDialog;
import android.app.Fragment;
import android.app.DialogFragment;
import java.util.Calendar;
import android.widget.TimePicker;

这是一个好Example

答案 1 :(得分:4)

最适合您的方法是使用this材料设计库来选择时间或日期并设置最小值。

    TimePickerDialog tpd = TimePickerDialog.newInstance(
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
                //put some interesting code
            }
        },
        now.get(Calendar.HOUR_OF_DAY),
        now.get(Calendar.MINUTE),
        true
    );
    tpd.setMinTime(10, 0, 0); // MIN: hours, minute, secconds
    tpd.show(getFragmentManager(), "TimePickerDialog");
  

将Material DateTime Picker库添加到项目的最简单方法是将其作为依赖项添加到build.gradle

dependencies {
    compile 'com.wdullaer:materialdatetimepicker:3.0.0'
}

希望这会对你有所帮助。

答案 2 :(得分:0)

ed_Totime=(EditText)dialog.findViewById(R.id.ed_Totime);
        ed_Totime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_error.setText("");
                final Calendar mcurrentTime = Calendar.getInstance();
                final int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                final  int minute = mcurrentTime.get(Calendar.MINUTE);
                TimePickerDialog mTimePicker;
                mTimePicker = new TimePickerDialog(QuarterMasterActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {

                        if (selectedHour>=hour && selectedMinute>=minute){
                            ed_Totime.setText( selectedHour + ":" + selectedMinute);
                        }
                        else{
                            text_error.setText("Invalid time! Please Select greater than current time");
                        }
                    }
                }, hour, minute, false);//Yes 24 hour time
                mTimePicker.setTitle("Select From Time");
                mTimePicker.show();
            }
        });