カスタムの日付ピッカー ビューで問題に直面しています。テキストビューから選択した日付を再度クリックすると、日付が空白で表示されることがあります。
この記事から参照しました: https://readyandroid.wordpress.com/datepicker-with-customization-android/
問題 :
コード :
private fun showDatePickerDialog() {
val cal = Calendar.getInstance()
val dialogDatePicker =
Dialog(context!!, R.style.LightDialogTheme)
dialogDatePicker.window
?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
dialogDatePicker.window
?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialogDatePicker.setCancelable(true)
val pickerView: View =
LayoutInflater.from(context!!).inflate(R.layout.dialog_datepicker, null)
val dPicker = pickerView.findViewById<DatePicker>(R.id.datePicker)
DatePickerUtil.setFont(activity!!, dPicker, pickerView)
DatePickerUtil.setDividerColor(dPicker, resources.getColor(R.color.btnColor))
// dPicker.maxDate = cal.timeInMillis
val formatnew =
SimpleDateFormat("EE, dd MMM yyyy", Locale.getDefault())
val cal1 = Calendar.getInstance()
// cal1.add(Calendar.MONTH, 6)
cal1.set(Calendar.MONTH, 11); // 11 = december
cal1.set(Calendar.DAY_OF_MONTH, 31);
dPicker.maxDate=cal1.timeInMillis
if (selectedDate1 == "") {
cal.set(Calendar.MONTH, 0); // 11 = december
cal.set(Calendar.DAY_OF_MONTH, 1);
dPicker.minDate = cal.timeInMillis
} else {
val date = formatnew.parse(selectedDate1)
dPicker.minDate = date.time
cal1.timeInMillis=date.time
cal1.set(Calendar.MONTH, 11); // 11 = december
cal1.set(Calendar.DAY_OF_MONTH, 31);
// cal1.add(Calendar.MONTH, 6)
dPicker.maxDate=cal1.timeInMillis
}
dPicker.init(
Calendar.getInstance()[Calendar.YEAR],
Calendar.getInstance()[Calendar.MONTH],
Calendar.getInstance()[Calendar.DAY_OF_MONTH]
) { view: DatePicker?, year: Int, monthOfYear: Int, dayOfMonth: Int ->
val selected = Calendar.getInstance()
selected[Calendar.YEAR] = year
selected[Calendar.MONTH] = monthOfYear
selected[Calendar.DAY_OF_MONTH] = dayOfMonth
val format =
SimpleDateFormat("EE, dd MMM yyyy", Locale.getDefault())
val format1 =
SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
selectedDate = format.format(selected.time)
receivedDate = format1.format(selected.time)
val sdf = SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
Locale.getDefault()
)
/*val today = Date()
val c: Calendar = Calendar.getInstance()
c.time = today
c.add(Calendar.MONTH, 6) // Add 6 months
val maxDate: Long = c.time.time
dPicker.maxDate = maxDate*/
view?.let { DatePickerUtil.setFont(activity!!, it, pickerView) }
}
if (!TextUtils.isEmpty(cetLeaveFromDate.editText?.text.toString())) {
val date: Date? =
parseDateMonthYear(cetLeaveFromDate.editText?.text.toString().trim())
if (date != null) {
day = DateFormat.format("dd", date) as String
month = DateFormat.format("MM", date) as String
year = DateFormat.format("yyyy", date) as String
}
if (year != null || month != null || day != null) dPicker.updateDate(
year!!.toInt(),
month!!.toInt() - 1,
day!!.toInt()
)
}
pickerView.findViewById<View>(R.id.btnOkay)
.setOnClickListener { view: View? ->
dialogDatePicker.dismiss()
if (selectedDate == "") {
if (selectedDate1 == "") {
val format =
SimpleDateFormat("EE, dd MMM yyyy", Locale.getDefault())
val format1 =
SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
selectedDate = format.format(Calendar.getInstance().time)
receivedDate = format1.format(Calendar.getInstance().time)
} else {
val format =
SimpleDateFormat("EE, dd MMM yyyy", Locale.getDefault())
val format1 =
SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
selectedDate = selectedDate1
receivedDate = selectedDate1
}
}
cetLeaveFromDate.setText(selectedDate)
checkButtonStatusListener()
}
dialogDatePicker.setContentView(pickerView)
if (!dialogDatePicker.isShowing) dialogDatePicker.show()
}