多个DatePickers动态地在同一个活动android中

时间:2013-12-31 08:57:09

标签: android datepicker

我已经阅读了很多关于创建datepicker的文章,但在我的情况下,我必须动态创建多个datepickers并在多个相应的EditTexts中更新日期。但在我的情况下,日期仅在最后一个EditText更新。 EditText和datepicker的数量可能会有所不同,但在这里我需要两个。我附上了快照。这是我的示例代码。

 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(""));
    }

我的代码出了什么问题。 提前谢谢。

enter image description here

2 个答案:

答案 0 :(得分:1)

使用以下代码,

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

我也陷入同样的​​问题,我使用@Adam L提供的答案,Multiple DatePickers in same activity ...

希望它会对你有所帮助。

答案 1 :(得分:1)

我遇到了同样的问题,我使用了以下代码。只需为每个datepicker对话框使用id,并为每个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());
        }
    });