如何禁用Google Material DatePicker(1.1.0-alpha10)的键盘输入方式?
Hey there! I’ve got you covered on disabling keyboard input for the Google Material DatePicker in version com.google.android.material:material:1.1.0-alpha10. Since this early alpha version doesn’t have an official API toggle for this, we’ll use a reliable workaround by targeting the underlying input field directly.
Feasible Solutions
Option 1: Traverse the View Tree to Disable the Input Field
The Material DatePicker uses an EditText internally for manual date entry. We can find this field right after the picker shows up and modify its properties to block keyboard input:
val datePicker = MaterialDatePicker.Builder.datePicker().build() // Add a listener to run code immediately after the picker is displayed datePicker.addOnShowListener { dialog -> // Get the root view of the date picker component val rootPickerView = dialog.findViewById<View>(com.google.android.material.R.id.material_date_picker) // Recursive function to locate the EditText inside the picker's view hierarchy fun findDateInputEditText(view: View): EditText? { if (view is EditText) { // Optional check to confirm it's the date input field return if (view.hint?.contains("date", ignoreCase = true) == true) view else null } if (view is ViewGroup) { for (i in 0 until view.childCount) { val foundEditText = findDateInputEditText(view.getChildAt(i)) if (foundEditText != null) return foundEditText } } return null } // Modify the EditText to block all manual input and keyboard popups findDateInputEditText(rootPickerView)?.apply { isFocusable = false isFocusableInTouchMode = false inputType = android.text.InputType.TYPE_NULL // Optional: Disable long clicks to prevent paste actions isLongClickable = false } } // Display the date picker datePicker.show(supportFragmentManager, "MATERIAL_DATE_PICKER")
Option 2: Use Reflection (Less Recommended but Direct)
If you prefer a more direct approach (note: this carries slight risk if internal component names change in future versions), you can use reflection to access the internal DatePicker and disable its input field:
datePicker.addOnShowListener { dialog -> try { val datePickerField = dialog.javaClass.getDeclaredField("datePicker") datePickerField.isAccessible = true val internalDatePicker = datePickerField.get(dialog) as DatePicker // Locate the system's built-in input EditText val inputField = internalDatePicker.findViewById<EditText>(android.R.id.input) inputField?.apply { isFocusable = false isFocusableInTouchMode = false } } catch (e: Exception) { // Handle reflection exceptions gracefully e.printStackTrace() } }
Key Notes
- The view traversal method is more stable across minor version updates, as it doesn’t depend on internal field names that might change.
- Both methods will prevent the soft keyboard from appearing and block all manual text entry in the date field.
- Keep an eye on future Material Component releases—official APIs (like a
setInputEnabled(false)method) might be added in later versions to simplify this.
Hope this solves your problem! Feel free to reach out if you hit any snags.
内容的提问来源于stack exchange,提问作者milton titus

