TextInputEditText - 可点击但不可编辑

3

我有一个布局,想要显示一些编辑框和日期选择器。由于我希望所有字段的设计都相同,因此我想到需要使用可点击但不可编辑的编辑框来作为日期选择器。以下是将用于显示日期选择器的我的编辑框的XML代码:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/notification_layout"
    style="@style/Theme.Connect.InputFieldLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:startIconDrawable="@drawable/ic_stk_notification_24dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/notification_edit_text"
        style="@style/Theme.Connect.InputField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Notification"
        android:maxLines="1"
        android:lines="1"
        android:cursorVisible="false"/>

</com.google.android.material.textfield.TextInputLayout>

我尝试了在互联网上找到的很多解决方案,我确实成功地使编辑文本不可编辑了,但没有涟漪点击效果。有没有办法使编辑文本不可编辑但仍保留涟漪点击效果?这是展示包含表单布局设计的日期选择器的最佳实践吗?

2个回答

3
 Try this and change the `android:focusable` attribute to `false`                       


<com.google.android.material.textfield.TextInputLayout
      android:id="@+id/notification_layout"
       style="@style/Theme.Connect.InputFieldLayout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:startIconDrawable="@drawable/ic_stk_notification_24dp">

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/notification_edit_text"
    style="@style/Theme.Connect.InputField"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Notification"
    android:maxLines="1"
    android:focusable="false"
    android:lines="1"
    android:cursorVisible="false"/>

</com.google.android.material.textfield.TextInputLayout>

感谢您的回答。我尝试了您的答案,但仍无法像通常的MaterialAutoCompleteTextView一样实现涟漪效果。 - George
如果您能发送一个示例,那将更有帮助。 - The Codepreneur

2
你可以简单地使用 TextView 替代 EditText,并为其设置一个 onClickListener()。你还可以按照自己的喜好进行样式设置。
notification_textview.setOnClickListener {
    // Whatever you want to do after a click
}

如果想在TextView上实现触摸时的涟漪效果,可以添加以下两个属性:

<......Textview
android:background=?android:attr/selectableItemBackground
android:clickable="true" />

你也可以为想要用于 editText 的文本视图添加样式。

谢谢您的回答。如果我使用文本视图,最终会得到不同的设计字段。此外,文本视图将没有涟漪点击效果,对吗? - George
@George 你可以为TextView使用自定义样式,但需要进行更多的研究来完成此操作。 - prayutsu
在 TextInputEditText 中使用 android:background=?android:attr/selectableItemBackground,可以得到涟漪效果。谢谢。 - satuser

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接