2

嗨,我在三个以上的类中使用日期选择器并具有相同的验证。相反,我必须在一个类中编写并在需要时在其他类中调用该函数,是否可以在日期选择器的代码下方。

    public class MainActivity extends Activity {

    private TextView tvDisplayDate;

    private Button btnChangeDate;

    private int myear;
    private int mmonth;
    private int mday;

    static final int DATE_DIALOG_ID = 999;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setCurrentDateOnView();
        addListenerOnButton();

    }

    // display current date
    public void setCurrentDateOnView() {

        tvDisplayDate = (TextView) findViewById(R.id.tvDate);       

        final Calendar c = Calendar.getInstance();
        myear = c.get(Calendar.YEAR);
        mmonth = c.get(Calendar.MONTH);
        mday = c.get(Calendar.DAY_OF_MONTH);

        // set current date into textview
        tvDisplayDate.setText(new StringBuilder()
                // Month is 0 based, just add 1
                .append(mmonth + 1).append("-").append(mday).append("-")
                .append(myear).append(" "));
    }

    public void addListenerOnButton() {

        btnChangeDate = (Button) findViewById(R.id.btnChangeDate);

        btnChangeDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                showDialog(DATE_DIALOG_ID);

            }

        });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            DatePickerDialog _date =   new DatePickerDialog(this, datePickerListener, myear,mmonth,
                    mday)
            {
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {   
                    if (year < myear)
                        view.updateDate(myear, mmonth, mday);

                    if (monthOfYear < mmonth && year == myear)
                        view.updateDate(myear, mmonth, mday);

                    if (dayOfMonth < mday && year == myear && monthOfYear == mmonth)
                        view.updateDate(myear, mmonth, mday);

                }
            };
            return _date;
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            myear = selectedYear;
            mmonth = selectedMonth;
            mday = selectedDay;

            // set selected date into textview
            tvDisplayDate.setText(new StringBuilder().append(mmonth + 1)
                    .append("-").append(mday).append("-").append(myear)
                    .append(" "));    

            Date dateObject1 = new Date(myear - 1900, mmonth, mday);

            Date dateObj2 = new Date(System.currentTimeMillis());

            if(dateObject1.before(dateObj2) || dateObject1.equals(dateObj2)){
                //the program runs normally
                }
                else{

                                new AlertDialog.Builder(MainActivity.this)

                                .setTitle("Wrong Data Input!")

                                .setMessage("The end Date must be Before the start Date, please insert new Date values")

                                .setNeutralButton("Ok",

                                new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog,

                                int which) {

                                }

                                }).show();
                            }


        }
    };

}
4

1 回答 1

0

你可以有一个DateUtils带有静态方法的类,它接受日期和上下文。此方法可以进行日期验证并AlertDialog相应地构建。您可以通过这种方式调用此静态方法。

DateUtils.dateValidator(Date, Context);

这可以在onDateSet()你的OnDateSetListenerfor each中调用DatePicker

于 2013-09-30T11:14:36.527 回答