如何在android中禁用未来的日期选择器

我是android的新手
如何在日期选择器中禁用未来日期,我在堆栈中尝试了许多代码,但它无法帮助.任何人都可以帮助我请求.

看看代码

public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.daty);



                click=(ImageView)findViewById(R.id.click);


                hdate=(TextView)findViewById(R.id.hdate);

                timeStamp = new SimpleDateFormat("dd/MM/yyyy").format(new Date(System.currentTimeMillis()));


                hdate.setText(timeStamp);


                final Calendar myCalendar = Calendar.getInstance();

       // listener for date picker        
                final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                            int dayOfMonth) {
                        // TODO Auto-generated method stub
                        myCalendar.set(Calendar.YEAR, year);
                        myCalendar.set(Calendar.MONTH, monthOfYear);
                        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        updateLabel();
                    }

                    private void updateLabel() {
                        // TODO Auto-generated method stub

                        String myFormat = "dd/MM/yyyy";
                        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                        hdate.setText(sdf.format(myCalendar.getTime()));


                    }

                };

//texview listener
                hdate.setOnClickListener(new OnClickListener() {


                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        new DatePickerDialog(Daty.this, date, myCalendar
                                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH)).show();


                    }


                });

解决方法:

您只需将setMaxDate函数用于日期选择器即可.

DatePickerDialog datePickerDialog = new DatePickerDialog(getApplicationContext(), date, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH);  //date is dateSetListener as per your code in question
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());

参见documentation

希望这可以帮助.

上一篇:weui中的picker使用js进行动态绑定数据


下一篇:php – date()返回1970-01-01