简体   繁体   中英

Android Date Picker help!

i need a little help here.
In my app a user need`s select one date interval (when press one button), and i'm not know how to do this using date picker!

i'm following this example: http://developer.android.com/resources/tutorials/views/hello-datepicker.html , but i have two questions about this tutorial...

1) How can i do which onClick event called DatePickerDialog.OnDateSetListener ( in the case where i have two EditText that when user click in wich one, the app shows the Date picker Dialog)

2) How can i call the onClick two times after user press one button?

tnhx! Sorry for poor English!

So from your explanation, you want to create a date interval picker from just one click on a button. By using analogy from your example you have to do the following:

// create two constants
static final int DATE_DIALOG_FROM = 0;
static final int DATE_DIALOG_TO = 1;

// create case for two pickers
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_FROM:
        return new DatePickerDialog(this, fromDateSetListener, mYear, mMonth,mDay);
    case DATE_DIALOG_TO:
        return new DatePickerDialog(this, toDateSetListener, mYear, mMonth,mDay);   
    }
    return null;
}

// create two listeners for both of the cases
private DatePickerDialog.OnDateSetListener fromDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Toast.makeText(AccountsActivity.this,"From:" + year+"."+monthOfYear+"."+dayOfMonth, Toast.LENGTH_SHORT).show();
        showDialog(DATE_DIALOG_TO);
    }
};

private DatePickerDialog.OnDateSetListener toDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
        Toast.makeText(AccountsActivity.this,"To:" + year+"."+monthOfYear+"."+dayOfMonth, Toast.LENGTH_SHORT).show();
        updateDisplay();
    }
};

When you run your app, second picker will appear right after finishing with first.

Later you can optimize this code, add custom pickers etc... If anyone knows a better way to create a DATE INTERVAL PICKER please let us know!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM