android中的动态TimePicker对话框

时间:2017-04-05 04:52:53

标签: android timepicker

我要求在android中创建一个动态时间选择器对话框。基本上每次从日历中选择日期时,时间都会发生变化,每次都必须更改范围。因此,它意味着在日期4/5/2017中,时间范围是从9到2,最小值必须是9,最大值必须是2,增量为1。 到目前为止我所做的只是设置一个时间选择器对话框

private void calltimerange() {
        Calendar mcurrentTime = Calendar.getInstance();
        int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
        int minute = mcurrentTime.get(Calendar.MINUTE);
        final TimePickerDialog mTimePicker;

        mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
              //  Toast.makeText(context, "" + selectedHour + ":" + selectedMinute, Toast.LENGTH_SHORT).show();
                tvTime.setText(String.valueOf(selectedHour));
                mytime = String.valueOf(selectedHour);
            }

        }, hour, minute, true);//Yes 24 hour time
        mTimePicker.setTitle("Select Time");
        mTimePicker.show();

    }

任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:0)

如果你想要的话,我建议你使用:

MaterialDateTimePicker by wdullaer

并寻找此功能:

  • setMinTime(时间点时间)

    设置要选择的最短有效时间。当天早些时候的时间价值 被停用

  • setMaxTime(时间点时间)

    设置要选择的最长有效时间。当天晚些时候的时间价值将是 停用

  • setSelectableTimes(Timepoint []次)

    您可以传入一组时间点。这些值是唯一有效的 选择器中的选择。 setMinTime(Timepoint time)和setMaxTime(Timepoint 时间)将进一步减少此列表。

答案 1 :(得分:0)

private void editTextDatePicker() {

    final EditText et_datepicker = new EditText(this);
    final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
    et_datepicker.setLayoutParams(lparams);
    et_datepicker.setPadding(20, 10, 20, 10);
    et_datepicker.setHint("Pick your Date");
    et_datepicker.setFocusable(false);
    et_datepicker.setGravity(1);
    et_datepicker.setBackgroundResource(R.drawable.edittext_background);
    Calendar newCalendar = Calendar.getInstance();
    final DatePickerDialog datepicker = new DatePickerDialog(this, R.style.dialogTheme, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            et_datepicker.setText(dateFormatter.format(newDate.getTime()));
        }

    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    et_datepicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            datepicker.show();
        }
    });
    linerLayoutChild.addView(et_datepicker); // here lineaLayoutChild is the object for your layout where you want to add. 
}