我需要在datepicker对话框中返回选中的值,请查看以下编码,
这是我的适配器类,我使用了datepicker对话框
if (holder.mClientNextvisit.isChecked() == true) {
holder.SelectDateLayout.setVisibility(View.VISIBLE);
holder.mClientSelectDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar calender = Calendar.getInstance();
year = calender.get(Calendar.YEAR);
month = calender.get(Calendar.MONTH);
day = calender.get(Calendar.DAY_OF_MONTH);
Log.d("Selected date", "---->" + day);
DatePickerDialog dialog = new DatePickerDialog(context, new DateListener(), year, month, day);
dialog.show();
}
});
}
这是我的datesetlistener类
class DateListener implements DatePickerDialog.OnDateSetListener {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
int mYear = year;
int mMonth = monthOfYear;
int mDay = dayOfMonth;
StringBuilder date = new StringBuilder().append(mMonth + 1)
.append("/").append(mDay).append("/").append(mYear)
.append(" ");
Log.d("date", "---->" + date);
}
现在我需要在适配器类中返回所选日期,请帮助我。
答案 0 :(得分:2)
如何使用匿名类?
DatePickerDialog dialog = new DatePickerDialog(context,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2,
int arg3) {
int mYear = year;
int mMonth = monthOfYear;
int mDay = dayOfMonth;
StringBuilder date = new StringBuilder()
.append(mMonth + 1).append("/").append(mDay)
.append("/").append(mYear).append(" ");
Log.d("date", "---->" + date);
}
}, year, month, day);
答案 1 :(得分:1)
假设mHolder
为TextView
传递对您的侦听器的引用并直接使用它。
class DateListener implements DatePickerDialog.OnDateSetListener {
private TextView mHolder;
public DateListener(TextView mHolder) { this.mHolder = mHolder; }
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
int mYear = year;
int mMonth = monthOfYear;
int mDay = dayOfMonth;
StringBuilder date = new StringBuilder().append(mMonth + 1)
.append("/").append(mDay).append("/").append(mYear)
.append(" ");
Log.d("date", "---->" + date);
mHolder.setText(date);
}
DatePickerDialog dialog = new DatePickerDialog(
context, new DateListener(mHolder), year,month, day);
dialog.show();