我的布局中有两个Textview和两个按钮,我想点击按钮弹出数据选择器并选择最终设置为特定textview的日期。
E.g
Button1选择日期设置为Textview1 Button2 chhose日期设置为textview2
但问题是第一次正常工作但如果我尝试第二次尝试更改button1但值设置为Textview2。
第一次只是工作好.Plz指导我,我一步步为基本问题而言更加精简。
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
tvDisplayDate2 = (TextView) findViewById(R.id.tvDate2);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
tvDisplayDate2.setText(tvDisplayDate.getText().toString());
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
btnChangeDate2 = (Button) findViewById(R.id.btnChangeDate2);
btnChangeDate2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID2);
}
});
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
System.out.println("onCreateDialog : " + id);
cur = DATE_DIALOG_ID;
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
case DATE_DIALOG_ID2:
cur = DATE_DIALOG_ID2;
System.out.println("onCreateDialog2 : " + id);
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
if (cur == DATE_DIALOG_ID) {
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
System.out.println("A");
System.out.println(cur);
}
if (cur == DATE_DIALOG_ID2){
tvDisplayDate2.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
System.out.println("B");
System.out.println(cur);
}
}
};
的ShowDialog();已弃用的消息
onCreateDialog已弃用的消息
showDialog()参数在点击时不会改变
答案 0 :(得分:0)
private DatePickerDialog.OnDateSetListener datePickerListener1 = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
private DatePickerDialog.OnDateSetListener datePickerListener2 = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
tvDisplayDate2.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
}
};
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
System.out.println("onCreateDialog : " + id);
cur = DATE_DIALOG_ID;
// set date picker as current date
return new DatePickerDialog(this, datePickerListener1, year, month,
day);
case DATE_DIALOG_ID2:
cur = DATE_DIALOG_ID2;
System.out.println("onCreateDialog2 : " + id);
// set date picker as current date
return new DatePickerDialog(this, datePickerListener2, year, month,
day);
}
return null;
}
答案 1 :(得分:0)
使用以下代码使用日期选择器设置日期
btnChangeDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calender = Calendar.getInstance();
int mYear = calender.get(Calendar.YEAR);
int mMonth = calender.get(Calendar.MONTH);
int mDay = calender.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(SubscriptionActivity.this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
/* Your code to get date and time */
tvDisplayDate.setText((selectedmonth + 1) + "-"
+ selectedday + "-" + selectedyear);
}
}, mYear, mMonth, mDay);
mDatePicker.show();
}
});
答案 2 :(得分:0)
只需将开关放入开关盒
break;
最终代码是
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
System.out.println("onCreateDialog : " + id);
cur = DATE_DIALOG_ID;
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
break;
case DATE_DIALOG_ID2:
cur = DATE_DIALOG_ID2;
System.out.println("onCreateDialog2 : " + id);
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
break;
}
return null;
}
答案 3 :(得分:0)
btnChangeDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
tv_data.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});