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

我在尝试使用某个日期选择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不更新Android中的Textview

同一活动中的多个DatePickers

你有

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

正如我所见,你只需要更改调用对话框ID,inheritance代码:

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

更改END_DATE_DIALOG_ID = 1;

试试它应该工作

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