gpt4 book ai didi

android - 如何在 DatePickerDialog Android 中禁用今天日期之前的日期

转载 作者:行者123 更新时间:2023-11-30 01:24:38 25 4
gpt4 key购买 nike

我想在 DatePickerDialog 中禁用今天日期之前的日期。我是 android 的新手,请建议我该怎么做。这是我为 DatePickerDialog 编写的代码。我正在扩展 Fragment 类。请帮助我:)

public class OneFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_one, container, false);


CheckInDate = (EditText) rootView.findViewById(R.id.CheckInDate);

CheckOutDate = (EditText) rootView.findViewById(R.id.CheckOutDate);

CheckInDate.setOnClickListener(new View.OnClickListener()

{
@Override
public void onClick(View v) {
if (v == CheckInDate) {

final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);


DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {

year = selectedYear;
month = selectedMonth +1;
day = selectedDay;


// set selected date into textview
CheckInDate.setText(new StringBuilder().append(day)
.append("/").append(month).append("/").append(year)
.append(" "));



}
}, year, month, day);
datePickerDialog.show();
}
}
});

CheckOutDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if (v == CheckOutDate) {

final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);


DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {

CheckOutDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

}
}, year, month, day);
datePickerDialog.show();
}
}
});
return rootView;
}}

最佳答案

使用DatePicker的setMinDate函数

datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
datePickerDialog.show();

工作代码如下

        final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH); // add here +1 if you don't
//want user to select current date also
c.set(Calendar.DAY_OF_MONTH, mDay);
DatePickerDialog dpd = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
cal.set(Calendar.YEAR, year);
if(cal.before(c)) {
//toast("Select valid date please.");
// notify user about wrong date.
return;
}
StringBuilder date = new StringBuilder();
//date.delete(0, date.length());
date.append((dayOfMonth<10?"0":"")).append(dayOfMonth)
.append("-").append((monthOfYear + 1) < 10 ? "0" : "")
.append((monthOfYear+1)).append("-").append(year);
//text.setText(date.toString());
}
}, mYear, mMonth, mDay);
dpd.getDatePicker().setMinDate(c.getTimeInMillis());
dpd.show();

希望它能奏效。

关于android - 如何在 DatePickerDialog Android 中禁用今天日期之前的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36564606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com