当我点击日期时,我正在变老1990年

时间:2017-09-07 10:35:54

标签: android

即使在写完dd.getDatePicker().setMaxDate(System.currentTimeMillis());

之后,我也渐渐变老了

有人可以帮助我吗

public class ForCalendar {

Calendar cal=Calendar.getInstance();

public ForCalendar(Context ct, final View v)
{

    SimpleDateFormat dateFormat= new SimpleDateFormat("dd-MM-yyyy");
    String strDate=dateFormat.format(cal.getTime());
    try {
        cal.setTime(dateFormat.parse((String)v.getTag()));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    DatePickerDialog dd=new DatePickerDialog(ct, new DatePickerDialog.OnDateSetListener()
    {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
        {
            EditText ed=(EditText)v;
            ed.setText(dayOfMonth+"-"+(month+1)+"-"+year);
            ed.setTag(dayOfMonth+"-"+(month+1)+"-"+year);

        }
    },cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DATE));
    dd.getDatePicker().setMaxDate(System.currentTimeMillis());
    dd.show();
}

}

2 个答案:

答案 0 :(得分:0)

     v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DatePickerFragment mDatePicker = new DatePickerFragment();
                        mDatePicker.show(getActivity().getFragmentManager(),"Select Date");
                    }
                });

   public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState)
        {
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }
        public void onDateSet(DatePicker view, int year, int month, int day)
        {
            if(month==monthh && day<dayy &&year<=yearr)
            {
                // your message. dont use dialogmessage. its my custom dialog. use in your away
               com.Toast_Short("Please Set Comming Date");
                end_date.performClick();
            }
            else if(month<=monthh && day==dayy && year<=yearr)
            {
                com.Toast_Short("Please Set Comming Date");
                end_date.performClick();
            }
            else if(month<=monthh && day<=dayy && year<yearr)
            {
                com.Toast_Short("Please Set Comming Date");
                end_date.performClick();
            }
            else
            {
                view.setMinDate(System.currentTimeMillis() - 1000);
                TimePicker mTimePicker = new TimePicker();
                mTimePicker.show(getFragmentManager(), "Select time");
                String mont , dy;
                if(month<10)
                    mont="0"+month;
                else mont=month+"";

                if(day<10)
                    dy="0"+day;
                else dy=day+"";

                v.setText( String.valueOf(dy) + "-" + String.valueOf(mont) + "-" + String.valueOf(year));
            }
        }
    }

答案 1 :(得分:0)

实际上问题在于以下几行代码

try {
    cal.setTime(dateFormat.parse((String)v.getTag()));
} catch (ParseException e) {
    e.printStackTrace();
}

你到底想要做什么。