如何使用多个日期选择器更新文本视图

时间:2013-12-23 07:33:53

标签: java android datepicker android-datepicker

我在尝试使用某个日期选择datepicker时尝试更新textview。但首先textview tahat是startDate不更新它总是更新第二个Text-view。我正在使用两个日期选择器来更新两个不同的textview。这是我更新TextViews的代码

public class AndroidDatePicker extends Activity {

private TextView mStartDate;
private TextView mEndDate;
private Button mStartBtn;
private Button mEndBtn;
int from_year, from_month, from_day, to_year, to_month, to_day;

static final int START_DATE_DIALOG_ID = 0;
static final int END_DATE_DIALOG_ID = 0;

static final int DATE_PICKER_TO = 0;
static final int DATE_PICKER_FROM = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.android_date_picker);

    mStartDate = (TextView) findViewById(R.id.textView1);
    mStartBtn = (Button) findViewById(R.id.button1);

    mStartBtn.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            showDialog(START_DATE_DIALOG_ID);
        }
    });



    mEndDate = (TextView) findViewById(R.id.textView2);
    mEndBtn = (Button) findViewById(R.id.button2);

    /* add a click listener to the button */
    mEndBtn.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            showDialog(END_DATE_DIALOG_ID);
        }
    });

    /* get the current date */
    final Calendar c = Calendar.getInstance();
    from_year = c.get(Calendar.YEAR);
    from_month = c.get(Calendar.MONTH);
    from_day = c.get(Calendar.DAY_OF_MONTH);
    to_year = c.get(Calendar.YEAR);
    to_month = c.get(Calendar.MONTH);
    to_day = c.get(Calendar.DAY_OF_MONTH);

    updateEndDisplay();
    updateStartDisplay();
}

private void updateEndDisplay() {
    mEndDate.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(to_month + 1).append("-").append(to_day).append("-")
            .append(to_year).append(" "));
}

private void updateStartDisplay() {
    mStartDate.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(from_month + 1).append("-").append(from_day)
            .append("-").append(from_year).append(" "));
}

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

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        from_year = year;
        from_month = monthOfYear;
        from_day = dayOfMonth;
        updateStartDisplay();
    }
};
private DatePickerDialog.OnDateSetListener to_dateListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        to_year = year;
        to_month = monthOfYear;
        to_day = dayOfMonth;
        updateEndDisplay();
    }
};

@Override
protected Dialog onCreateDialog(int id) {

    switch (id) {
    case DATE_PICKER_FROM:
        return new DatePickerDialog(this, from_dateListener, from_year,
                from_month, from_day);
    case DATE_PICKER_TO:
        return new DatePickerDialog(this, to_dateListener, to_year,
                to_month, to_day);
    }
    return null;
}
}

它只是更新text-view2而不是text-view1。我不知道为什么。我已经按照以下链接寻求解决方案,但它不适合我的情况我不知道为什么,请帮助我。

DatePicker not updating Textview in Android

Multiple DatePickers in same activity

4 个答案:

答案 0 :(得分:1)

你有

static final int START_DATE_DIALOG_ID = 0;
static final int END_DATE_DIALOG_ID = 0;

将其更改为

static final int START_DATE_DIALOG_ID = 0;
static final int END_DATE_DIALOG_ID = 1;

否则

showDialog(START_DATE_DIALOG_ID);

showDialog(END_DATE_DIALOG_ID);

仅根据您的代码显示DATE_PICKER_FROM(即index = 1)对话框

protected Dialog onCreateDialog(int id) {

    switch (id) {
    case DATE_PICKER_FROM:
        return new DatePickerDialog(this, from_dateListener, from_year,
                from_month, from_day);
    case DATE_PICKER_TO:
        return new DatePickerDialog(this, to_dateListener, to_year,
                to_month, to_day);
    }
    return null;
}

答案 1 :(得分:1)

正如我所看到的,你只需要更改调用对话框ID,继承代码:

    mStartBtn.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            showDialog(DATE_PICKER_FROM);
        }
    });
    mEndBtn.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            showDialog(DATE_PICKER_TO);
        }
    });

答案 2 :(得分:0)

变化 END_DATE_DIALOG_ID = 1;

尝试它应该工作

答案 3 :(得分:0)

只使用一个DatePickerDialog。 声明一个全局变量Boolean isFromDate; 在显示对话框时将其设置为true或false。 在onDateSet()方法中,检查isFromDate以确定要添加的标签