如何使用多个日期检查器更新文本视图 [英] How to update text-view with multiple datepickers
问题描述
在某些日期选择datepicker时,我正在尝试更新textview。但是第一个textview tahat是startDate没有更新它总是更新第二个文本视图。我正在使用两个日期选择器来更新两个不同的文本视图。这是我更新TextViews的代码
I am trying to update textview when datepicker is selected with some date. But first textview tahat is startDate is not updating it always update second Text-view. I am taking two Date Picker to update two different textview. Here is my code for updating the 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-视图1。我不知道为什么我已经遵循下面的链接解决方案,但它不工作在我的情况下,我不知道为什么,请帮助我。
It is just updating text-view2 but not text-view1. I don't know why. I have followed below link for solution but it is not working in my case i don't know why, Please help me.
推荐答案
你有
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)对话框
will show the DATE_PICKER_FROM (i.e index=1) dialog only according to your code
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;
}
这篇关于如何使用多个日期检查器更新文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!