3

我希望用户仅在该月内选择一个日期,并且不小于当前日期。

例如:如果这一天是 13 年 3 月 18 日,那么用户 shud 只选择从 13 年 3 月 18 日到 31 日 3 月 31 日的任何日期。此外,我有两个按钮用于选择“从日期”和“到日期”,它们使用相同的监听器进行日期选择器。我已经尝试了本网站上已经建议的不同解决方案,但在这种情况下它们不兼容。如果我使用建议的代码,我将失去对两个按钮使用相同日期选择器侦听器的功能。我尝试了下面的代码,但它不工作。

public class Leave_form extends Activity 
{


    private static EditText tvDislpayResult;
    private Button startPickDate;
    private Button endPickDate;
    private Calendar startDate;
    private Calendar endDate;
    //private EditText startDateDisplay;
    //private EditText endDateDisplay;
    static final int DATE_DIALOG_ID = 0;
    private TextView startDateDisplay;
    private TextView endDateDisplay;
    private TextView activeDateDisplay;
    private Calendar activeDate;



    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave_form);

        /*  capture our View elements for the start date function   */
        startDateDisplay = (EditText) findViewById(R.id.e_from_date);
        startPickDate = (Button) findViewById(R.id.Set_date1);

        /* get the current date */
        startDate = Calendar.getInstance();

        /* add a click listener to the button   */
        startPickDate.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) 
            {
                showDateDialog(startDateDisplay, startDate);
            }
        });

        /* capture our View elements for the end date function */
        endDateDisplay = (EditText) findViewById(R.id.e_to_date);
        endPickDate = (Button) findViewById(R.id.Set_date2);

        /* get the current date */
        endDate = Calendar.getInstance();

        /* add a click listener to the button   */
        endPickDate.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                showDateDialog(endDateDisplay, endDate);
            }
        });

        /* display the current date (this method is below)  */
       // updateDisplay(startDateDisplay, startDate);
       // updateDisplay(endDateDisplay, endDate);
    }

    private void updateDisplay(TextView dateDisplay, Calendar date) {
        dateDisplay.setText(
                new StringBuilder()
                    // Month is 0 based so add 1
                    .append(date.get(Calendar.DAY_OF_MONTH) ).append("-")
                    .append(date.get(Calendar.MONTH)+1).append("-")
                    .append(date.get(Calendar.YEAR)).append(" "));

    }

    public void showDateDialog(TextView dateDisplay, Calendar date)
    {
       activeDateDisplay = dateDisplay;
        activeDate = date;
        showDialog(DATE_DIALOG_ID);
    }

    private OnDateSetListener dateSetListener = new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
         {

             try {
                    Date selectedDate = new SimpleDateFormat("yyyy/MM/dd").parse(year+"/"+monthOfYear+"/"+dayOfMonth);
                    Date currDate = new Date();
                    if(selectedDate.compareTo(currDate ) >=0 )
                        {
                          activeDate.set(Calendar.YEAR, year);
                          activeDate.set(Calendar.MONTH, monthOfYear);
                          activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                          updateDisplay(activeDateDisplay, activeDate);
                          unregisterDateDisplay();

                        }
                    else
                        {
                         //show message
                        }

              }
              catch(Exception e)
              {
                  e.getMessage();

               }
                   }
    };

    private void unregisterDateDisplay() {
        activeDateDisplay = null;
        activeDate = null;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
        }
        return null;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        switch (id) {
            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
                break;
        }
    }

    public void leave(View view)
    {
        Intent intent1=new Intent(this,Rough.class);
        setContentView(R.layout.activity_rough);
        startActivity(intent1);


    }



}
4

0 回答 0