从Material Design Datepicker Android Kotlin获取月份的方法如下:
implementation 'com.google.android.material:material:1.4.0'
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/datepicker_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/datepicker_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Select Date"
android:inputType="none"
android:focusable="false"
android:clickable="true" />
</com.google.android.material.textfield.TextInputLayout>
val datePickerEditText = findViewById<EditText>(R.id.datepicker_edittext)
datePickerEditText.setOnClickListener {
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(this, { _, selectedYear, selectedMonth, selectedDay ->
val selectedDate = Calendar.getInstance()
selectedDate.set(selectedYear, selectedMonth, selectedDay)
val selectedMonth = selectedDate.get(Calendar.MONTH)
// 在这里获取到了选择的月份,可以进行后续操作
// 例如,可以将月份显示在EditText中
datePickerEditText.setText((selectedMonth + 1).toString())
}, year, month, day)
datePickerDialog.show()
}
这样,当用户点击EditText时,将弹出一个日期选择对话框。选择日期后,月份将显示在EditText中。
推荐的腾讯云相关产品:腾讯云移动开发平台(https://cloud.tencent.com/product/mpp)
领取专属 10元无门槛券
手把手带您无忧上云