【问题标题】:How to disable the dates after current date?如何禁用当前日期之后的日期?
【发布时间】:2019-04-27 20:47:47
【问题描述】:

帮助在日期选择器中禁用从当前日期开始的所有日期。
这是我的代码:

public class MyDatePickerDialogue extends DatePickerDialog{
    private CharSequence title;
    private Context context;

    public MyDatePickerDialogue(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {

        super(context, callBack, year, monthOfYear, dayOfMonth);
        this.context = context;
    }

    public void setPermanentTitle(CharSequence title) {
        this.title = title;
        setTitle(title);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        super.onDateChanged(view, year, month, day);
        setTitle(title);
        Toast.makeText( context,"Date picked", Toast.LENGTH_SHORT).show();
        SingletonClass.getSingletonClass().singletondate = year+"-"+(month+1)+"-"+day;
        //save the date that is picked into the singleton class
        //i represents year , i1 represents month and i2  represents day

        context.startActivity(new Intent(context,DescriptionActivity.class));

    }
}

【问题讨论】:

标签: android date android-datepicker


【解决方案1】:

您可以为 DatePicker 视图设置 maxDate

    Date today = new Date();
    Calendar c = Calendar.getInstance(TimeZone.getDefault());
    c.setTime(today);
    maxDate = c.getTime().getTime();
    view.setMaxDate(maxDate);

【讨论】:

  • 如果您认为这是正确答案,请在@Adil Sajjad Abbasi 上将其标记为正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 2018-08-27
  • 1970-01-01
相关资源
最近更新 更多