【问题标题】:Multiple DatePickers dynamically in same activity android多个DatePickers在同一个活动android中动态
【发布时间】:2013-12-31 20:03:36
【问题描述】:

我已经阅读了很多关于创建日期选择器的文章,但在我的情况下,我必须动态创建多个日期选择器并更新多个相应的 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(""));
    }

我的代码有什么问题。 提前致谢。

【问题讨论】:

  • 您提供的代码无法编译。您能否添加您实际运行的代码 - 目前很难理解它应该做什么以及它有什么问题。

标签: android datepicker


【解决方案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...

希望对你有帮助。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢您的回答,但我需要动态。在这里,您假设只有 2 个日期选择器/对话框(BIRTH_DATE_DIALOG 和 ANNIVERSARY_DATE_DIALOG),但我可能有任意数量的日期选择器。
    • 受保护的对话框 onCreateDialog(int id) 已弃用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多