简体   繁体   中英

Multiple DatePickers dynamically in same activity android

I have read many articles for creating datepicker but in my case I have to create multiple datepickers dynamically and update the date in multiple corresponding EditTexts. But in my case the date is updated only at the last EditText. The number of EditText and datepicker may vary but here I am taking two. I have attached the snapshot herewith. And here is my sample code.

 public datepicker(){
   EditText txtBox = new EditText(context);
   Button btn = new Button(context);
      btn.setOnClickListener(new Button.OnClickListener() {
     public void onClick(View v) {              
        DatePickerDialog dialog= new DatePickerDialog(act, mDateSetListener,
                       mYear, mMonth, mDay); 
            dialog.show();                              
        }
    });     

 }



    private DatePickerDialog.OnDateSetListener mDateSetListener = new   
    DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }

  private void updateDisplay() {    
    txtBox.setText(new StringBuilder().append(mMonth + 1).append("-")
     .append(mDay).append("").append(mYear).append(""));
    }

What's wrong in my code. Thanks in advance.

在此输入图像描述

Use below code,

 public class MultiDatePickerActivity extends Activity {

    private TextView startDateDisplay;
    private TextView endDateDisplay;
    private Button startPickDate;
    private Button endPickDate;
    private Calendar startDate;
    private Calendar endDate;

    static final int DATE_DIALOG_ID = 0;

    private TextView activeDateDisplay;
    private Calendar activeDate;

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

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

        /* 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 = (TextView) findViewById(R.id.endDateDisplay);
        endPickDate = (Button) findViewById(R.id.endPickDate);

        /* 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.MONTH) + 1).append("-")
                    .append(date.get(Calendar.DAY_OF_MONTH)).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) {
            activeDate.set(Calendar.YEAR, year);
            activeDate.set(Calendar.MONTH, monthOfYear);
            activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateDisplay(activeDateDisplay, activeDate);
            unregisterDateDisplay();
        }
    };

    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;
        }
    }
}

i was also stuck in same issue and i use this answer provided by @Adam L, here Multiple DatePickers in same activity ...

Hope it will help you.

I had same problem and I used following code. Just use id for each datepicker dialog and perform some action for each id.

 private final int BIRTH_DATE_DIALOG=4000;
 private final int ANNIVERSARY_DATE_DIALOG=5000;


@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub

    Calendar cal = Calendar.getInstance();
    DatePickerDialog datePicker = new DatePickerDialog(YourActivity.this,new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
                            EditText edt_Bdate=(EditText)findViewById(id);
                            edt_Bdate.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);


        }
    },cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
            cal.get(Calendar.DAY_OF_MONTH));
    return datePicker;
}

edittext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // pass id here
            showDialog(edittext.getId());
        }
    });

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