在Android Studio中两次之间

时间:2018-06-15 01:38:38

标签: java android

尝试在Android Studio中兼容两次。具体而言,下午5点和晚上11点。我可以让它在这些时间之间进入,但是当我选择一个时间不是晚上11点或下午5点时,它就变成了现实。有更简单的方法吗?

到目前为止,这是我的代码

Calendar c = Calendar.getInstance();
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        if(hourOfDay >= 23 && hourOfDay <= 17){
        reservation.setText("The time that your food will arrive will be " + timeFormat.format(c.getTime()) + ".");
        }else{
            reservation.setText("Please select a time between 5PM and 11PM");
        }
        //reservation.setText(hourOfDay + " " + minute);

    }

3 个答案:

答案 0 :(得分:0)

小时为23时检查分钟。

<div *ngFor="let language of model.appLanguages" style="display:inline">
<ng-container *ngIf="showDefaultFirst(text.languageId)">
    <input type="radio" name="title"
           id="language.languageId"
           [checked]="language.isDefault" /> {{ language.displayName }} 
</ng-container>
</div>

答案 1 :(得分:0)

if(hourOfDay >= 23 && hourOfDay <= 17){

一个hourOfDay如何同时为<= 17>= 23?尝试将&&切换为||

答案 2 :(得分:0)

您可以使用以下小部件显示带有范围设置的TimeDialog。

package com.mysuger.android.dialogs;

import java.lang.reflect.Field;
import java.text.DateFormat;
import java.util.Calendar;

import android.app.TimePickerDialog;
import android.content.Context;
import android.widget.TimePicker;

/**
 * A time dialog that allows setting a min and max time.
 * 
 */
public class RangeTimePickerDialog extends TimePickerDialog {

private int minHour = -1;
private int minMinute = -1;

private int maxHour = 25;
private int maxMinute = 25;

private int currentHour = 0;
private int currentMinute = 0;

private Calendar calendar = Calendar.getInstance();
private DateFormat dateFormat;


public RangeTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) {
    super(context, callBack, hourOfDay, minute, is24HourView);
    currentHour = hourOfDay;
    currentMinute = minute;
    dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);

    try {
        Class<?> superclass = getClass().getSuperclass();
        Field mTimePickerField = superclass.getDeclaredField("mTimePicker");
        mTimePickerField.setAccessible(true);
        TimePicker mTimePicker = (TimePicker) mTimePickerField.get(this);
        mTimePicker.setOnTimeChangedListener(this);
    } catch (NoSuchFieldException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    }
}

public void setMin(int hour, int minute) {
    minHour = hour;
    minMinute = minute;
}

public void setMax(int hour, int minute) {
    maxHour = hour;
    maxMinute = minute;
}

@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

    boolean validTime = true;
    if (hourOfDay < minHour || (hourOfDay == minHour && minute < minMinute)){
        validTime = false;
    }

    if (hourOfDay  > maxHour || (hourOfDay == maxHour && minute > maxMinute)){
        validTime = false;
    }

    if (validTime) {
        currentHour = hourOfDay;
        currentMinute = minute;
    }

    updateTime(currentHour, currentMinute);
    updateDialogTitle(view, currentHour, currentMinute);
}

private void updateDialogTitle(TimePicker timePicker, int hourOfDay, int minute) {
    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calendar.set(Calendar.MINUTE, minute);
    String title = dateFormat.format(calendar.getTime());
    setTitle(title);
}
 }

从下面提到的方式调用它,

    Calendar c = Calendar.getInstance();
    DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
    TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            c.set(Calendar.HOUR_OF_DAY, hourOfDay);
            c.set(Calendar.MINUTE, minute);
            reservation.setText("The time that your food will arrive will be " + timeFormat.format(c.getTime()) + ".");
        }
    RangeTimePickerDialog d = new RangeTimePickerDialog(Activity.this, t, 17, 0, true);
d.setMin(17, 0);
d.setMax(23, 0);
d.show();

有关详细信息,请参阅this