单击进入 Material 大纲文本字段时显示 MaterialDatePicker

Showing the MaterialDatePicker when clicking into the Material Outlined Text Field

我正在开发一个小应用程序,用户可以在其中启动 activity。 通过这样做,将在默认设置今天日期的位置显示文本输入。

单击文本字段时,MaterialDatePicker 应显示为对话框。

我已经成功实现了单击按钮时显示 MaterialDatePicker,但是我找不到显示对话框而不是 Android 标准 android 键盘的解决方案。

有什么建议吗?

Activity XML:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/edtStartWearLensDate"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="@string/txtLabelWearSelectStartDate"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtLabelStartWearLensID">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

Activity:

创建时:

edtStartWearLensDate = findViewById(R.id.edtStartWearLensDate);
edtStartWearLensDate.setOnClickListener(view -> onEdtStartWearLensDateClick());

点击进入文本字段

private void onEdtStartWearLensDateClick() {
        showDatePickerDialog();
}

显示对话框的方法(在单击侦听器时从按钮调用时有效)

private void showDatePickerDialog(){
        MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();

        builder.setTitleText("Select start date");
        builder.setSelection(MaterialDatePicker.todayInUtcMilliseconds());

        final MaterialDatePicker materialDatePicker = builder.build();

        materialDatePicker.show(getSupportFragmentManager(), "DATE_PICKER");

        materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
            @Override
            public void onPositiveButtonClick(Object selection) {
                edtStartWearLensDate.getEditText().setText(materialDatePicker.getHeaderText());
            }
        });
    }

尝试在 TextInputEditText:

中将 clickablefocusable 属性设置为 true
<com.google.android.material.textfield.TextInputEditText
            android:id="@+id/text_input_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:clickable="true"/>

并对其调用 setOnClickListener 方法:

findViewById<TextInputEditText>(R.id.text_input_edit_text).setOnClickListener(view -> onEdtStartWearLensDateClick())