DatePickerDialog
我想设置单击 Date 类型时出现的 a 的最小值和最大值EditText
,根据我的调查,可以这样做:
setMaxDate(long maxDate)
setMinDate(long minDate)
但我不知道如何将它应用到我的代码中,我的代码如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static EditText fechauso, horainicio;
private int anio, dia, mes, hora, minuto;
private static final int TIPO_DIALOGO = 0;
private static DatePickerDialog.OnDateSetListener oyenteSelectorFecha;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fechauso = (EditText) findViewById(R.id.txt_fechadeuso);
horainicio = (EditText) findViewById(R.id.txt_horainicio);
horainicio.setOnClickListener(this);
fechauso.setInputType(InputType.TYPE_NULL);
Calendar calendario = Calendar.getInstance();
anio = calendario.get(Calendar.YEAR);
mes = calendario.get(Calendar.MONTH);
dia = calendario.get(Calendar.DAY_OF_MONTH);
oyenteSelectorFecha = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
anio = year;
mes = monthOfYear;
dia = dayOfMonth;
mostrarFecha();
horainicio.requestFocus();
}
};
fechauso.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
closeSoftKeyBoard();
mostrarCalendario(fechauso);
}
}
});
horainicio.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
closeSoftKeyBoard();
mostrarTime();
}
}
});
}
public void mostrarCalendario(View control) {
showDialog(TIPO_DIALOGO);
}
public void mostrarFecha() {
fechauso.setText(anio + "-" + (mes + 1) + "-" + dia);
}
public void mostrarTime() {
// Get Current Time
final Calendar c = Calendar.getInstance();
hora = c.get(Calendar.HOUR);
minuto = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
horainicio.setText(hourOfDay + ":" + minute + ":00");
}
}, hora, minuto, false);
timePickerDialog.show();
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new DatePickerDialog(this, oyenteSelectorFecha, anio, mes, dia);
}
return null;
}
public void closeSoftKeyBoard() {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
@Override
public void onClick(View v) {
if (v == horainicio) {
closeSoftKeyBoard();
mostrarTime();
}
}
}
你必须得到
DatePicker
他和他来设定限制。您必须使用setMinDate(long maxDate)
和 setMaxDate(long minDate)
更新:
我记得这里有一个类似的问题如何在 android datepicker 中禁用天数?,这将是一个例子:
我们定义变量来配置我们的 DatePickerDialog:
这是一个创建 DatePicker 并定义最小日期的示例,例如我们将其定义为上个月和前一天:
因此,您的 DatePicker 将只允许至少在
calendarioMin.getTimeInMillis()
.