如何在 Kotlin(Android) 的日期选择器对话框中只显示日期和月份?
How to show only day and month in date picker dialog in Kotlin(Android)?
我有一个日期选择器对话框。我只想显示日期和月份。年份选择器对话框必须隐藏。我已经尝试过 this 等其他答案。什么都不适合我。它还应该支持 Kitkat 到 Nougat 设备。我的日期选择器代码如下。
fun setDatePickerDialog() {
mDobDialog = DatePickerDialog(this@SignUpActivity, R.style.VDDatePickerDialogTheme, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
val newDate = Calendar.getInstance()
newDate.set(year, monthOfYear, dayOfMonth)
//dob_textview.setText("($dayOfMonth) ($monthOfYear) ($year)")
val dateFormat = SimpleDateFormat(VDAppConstants.DOB_DISPLAY_FORMAT)
dob_textview?.setText(dateFormat.format(newDate.time))
}, mNewCalendar.get(Calendar.YEAR), mNewCalendar.get(Calendar.MONTH), mNewCalendar.get(Calendar.DAY_OF_MONTH))
mNewCalendar.set(1978,
mNewCalendar.get(Calendar.MONTH),
mNewCalendar.get(Calendar.DAY_OF_MONTH))
mDobDialog?.datePicker?.maxDate = mNewCalendar.timeInMillis
}
以下代码仅适用于 Kitkat 设备,不适用于 Nougat 设备。
val mDobDialog = DatePickerDialog(this@MainActivity, android.R.style.Theme_Holo_Dialog, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// Display Selected date in textbox
//date.setText("" + dayOfMonth + " " + monthOfYear + ", " + year)
}, year, month, day)
mDobDialog.show()
// Hide Year Selector in Date Picker
mDobDialog.findViewById(Resources.getSystem().getIdentifier("year", "id", "android")).visibility = View.GONE
我用不同的代码进行了测试,这段代码工作正常。如果您将日期选择器主题用作 Theme_Holo_Dialog 那么它工作正常。工作代码如下。
注意:如果你设置了主题是无效的Theme_Material_Dialog
package com.wave18.datepickedialogdemo
import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.content.res.Resources
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Date Picker Dialog
val dialog = datePickerDialog()
// Button for Showing Date Picker Dialog
button_show_date_picker.setOnClickListener {
// Show Date Picker
dialog.show()
// Hide Year Selector
val year = dialog.findViewById<View>(Resources.getSystem().getIdentifier("android:id/year", null, null))
if (year != null) {
year.visibility = View.GONE
}
}
}
// Function for Showing Date Picker
@SuppressLint("SetTextI18n")
fun datePickerDialog(): DatePickerDialog {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
// Date Picker Dialog
val datePickerDialog = DatePickerDialog(this@MainActivity, android.R.style.Theme_Holo_Dialog, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// Display Selected date in textbox
date.text = "$dayOfMonth $monthOfYear, $year"
}, year, month, day)
// Show Date Picker
return datePickerDialog
}
}
我有一个日期选择器对话框。我只想显示日期和月份。年份选择器对话框必须隐藏。我已经尝试过 this 等其他答案。什么都不适合我。它还应该支持 Kitkat 到 Nougat 设备。我的日期选择器代码如下。
fun setDatePickerDialog() {
mDobDialog = DatePickerDialog(this@SignUpActivity, R.style.VDDatePickerDialogTheme, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
val newDate = Calendar.getInstance()
newDate.set(year, monthOfYear, dayOfMonth)
//dob_textview.setText("($dayOfMonth) ($monthOfYear) ($year)")
val dateFormat = SimpleDateFormat(VDAppConstants.DOB_DISPLAY_FORMAT)
dob_textview?.setText(dateFormat.format(newDate.time))
}, mNewCalendar.get(Calendar.YEAR), mNewCalendar.get(Calendar.MONTH), mNewCalendar.get(Calendar.DAY_OF_MONTH))
mNewCalendar.set(1978,
mNewCalendar.get(Calendar.MONTH),
mNewCalendar.get(Calendar.DAY_OF_MONTH))
mDobDialog?.datePicker?.maxDate = mNewCalendar.timeInMillis
}
以下代码仅适用于 Kitkat 设备,不适用于 Nougat 设备。
val mDobDialog = DatePickerDialog(this@MainActivity, android.R.style.Theme_Holo_Dialog, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// Display Selected date in textbox
//date.setText("" + dayOfMonth + " " + monthOfYear + ", " + year)
}, year, month, day)
mDobDialog.show()
// Hide Year Selector in Date Picker
mDobDialog.findViewById(Resources.getSystem().getIdentifier("year", "id", "android")).visibility = View.GONE
我用不同的代码进行了测试,这段代码工作正常。如果您将日期选择器主题用作 Theme_Holo_Dialog 那么它工作正常。工作代码如下。
注意:如果你设置了主题是无效的Theme_Material_Dialog
package com.wave18.datepickedialogdemo
import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.content.res.Resources
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Date Picker Dialog
val dialog = datePickerDialog()
// Button for Showing Date Picker Dialog
button_show_date_picker.setOnClickListener {
// Show Date Picker
dialog.show()
// Hide Year Selector
val year = dialog.findViewById<View>(Resources.getSystem().getIdentifier("android:id/year", null, null))
if (year != null) {
year.visibility = View.GONE
}
}
}
// Function for Showing Date Picker
@SuppressLint("SetTextI18n")
fun datePickerDialog(): DatePickerDialog {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
// Date Picker Dialog
val datePickerDialog = DatePickerDialog(this@MainActivity, android.R.style.Theme_Holo_Dialog, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// Display Selected date in textbox
date.text = "$dayOfMonth $monthOfYear, $year"
}, year, month, day)
// Show Date Picker
return datePickerDialog
}
}