You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio 3.1版本XML布局中找不到DatePicker,是否有替代组件?

替代旧DatePicker的方案(Android Studio 3.1+)

Hey there! I totally get your frustration—losing access to a familiar component after an update can be a real headache. Let me walk you through the reliable alternatives you can use instead of the old DatePicker in Android Studio 3.1 and later versions.

1. 使用AppCompatDatePicker(嵌入布局首选)

This is the closest replacement to the old DatePicker you’re used to. It’s part of the AndroidX AppCompat library, supports direct use in XML layouts, and comes with built-in compatibility to keep the UI consistent across different Android versions.

快速上手:

  • First, make sure your project has the AppCompat dependency (most new projects include this by default; if not, add it to your module-level build.gradle):
implementation 'androidx.appcompat:appcompat:1.6.1'
  • Then add it directly to your XML layout:
<androidx.appcompat.widget.AppCompatDatePicker
    android:id="@+id/embeddedDatePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:datePickerMode="spinner" <!-- Choose between "spinner" or "calendar" mode -->
    android:calendarViewShown="true" />
  • Getting the selected date works almost exactly like the old DatePicker:
val datePicker = findViewById<AppCompatDatePicker>(R.id.embeddedDatePicker)
val selectedDay = datePicker.dayOfMonth
val selectedMonth = datePicker.month + 1 // Months are 0-indexed here
val selectedYear = datePicker.year

2. 使用Material Date Picker(弹窗式选择,官方推荐)

If you don’t need to embed the picker directly in your layout and prefer a pop-up selection flow, Google’s MaterialDatePicker is the modern, Material Design-compliant choice. It’s cleaner and more user-friendly for most use cases.

快速上手:

  • Add the Material Components dependency to your module-level build.gradle:
implementation 'com.google.android.material:material:1.9.0'
  • Trigger the picker from your code (Kotlin example):
val datePicker = MaterialDatePicker.Builder.datePicker()
    .setTitleText("Pick a Date")
    .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
    .build()

datePicker.show(supportFragmentManager, "DATE_PICKER_DIALOG")

// Listen for the user's selection
datePicker.addOnPositiveButtonClickListener { utcTimestamp ->
    // Convert the UTC timestamp to a readable date format
    val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
    calendar.timeInMillis = utcTimestamp
    val formattedDate = "${calendar.get(Calendar.DAY_OF_MONTH)}/${calendar.get(Calendar.MONTH)+1}/${calendar.get(Calendar.YEAR)}"
    // Do something with the selected date here
}

Why did the old DatePicker disappear from the component panel?

The old android.widget.DatePicker isn’t actually gone—you can still manually write the XML code to use it. But starting with Android Studio 3.1, Google stopped showing it in the layout editor’s component panel because it has inconsistent styling across Android versions and lacks the compatibility features of the AppCompat/Material alternatives.


内容的提问来源于stack exchange,提问作者Ali Bavi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:20:03