如何在TextView中设置开始日期和结束日期,以及如何在TextView上设置日期监听器?
tv_fromDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv_fromDate.setText(getCurrentDate());
}
});
tv_toDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv_toDate.setText(getCurrentDate());
}
});
答案 0 :(得分:0)
使用SimpleDateFormat进行格式化,使用Calendar类进行日期相关操作,使用DatePickerDialog在TextView上单击选择日期。
<强>更新:强> 当前代码中需要进行一些更改:
tv_fromDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getCurrentDate(tv_fromDate);
}
});
tv_toDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getCurrentDate(tv_toDate);
}
});
这是方法中需要的代码:
public void getCurrentDate(TextView tvDate) {
DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(datePicker, year, monthOfYear, dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
tvDate.setText(simpleDateFormat.format(calendar.getTime()));
}
}, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}