[英]TextInputEditText - Make clickable but not editable
I have a layout where I want to show a bunvh of edit texts and a date picker.我有一个布局,我想在其中显示一组编辑文本和一个日期选择器。 Since I want to have the same design for all fields, I figured out that I need to user an edit text for date picker too but make it non editable but clickable.由于我想对所有字段进行相同的设计,我发现我也需要为日期选择器使用编辑文本,但使其不可编辑但可点击。 Below is the xml code of my edit text that will be used to show the date picker:下面是用于显示日期选择器的编辑文本的 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>
I tried many solutions I found on the internet, I did managed to make edit text non editable but without the ripple click effect.我尝试了在互联网上找到的许多解决方案,我确实设法使编辑文本不可编辑,但没有波纹点击效果。 Is there a way to make the edit text non edditable but still keep the ripple click effect?有没有办法使编辑文本不可编辑但仍保持波纹点击效果? Is this the best practice to show a date picker consisting the design of the form layout?这是显示包含表单布局设计的日期选择器的最佳实践吗?
You can simply use a TextView
instead of an EditText
and you can set an onClickListener()
to it.您可以简单地使用TextView
而不是EditText
,并且可以为其设置 onClickListener onClickListener()
。 You can also style it the way you want.你也可以按照你想要的方式来设计它。
notification_textview.setOnClickListener {
// Whatever you want to do after a click
}
You can get a ripple effect on touch by adding these 2 attributes to your textview您可以通过将这 2 个属性添加到 textview 来获得触摸涟漪效果
<......Textview
android:background=?android:attr/selectableItemBackground
android:clickable="true" />
You can also add styles to a textview that you wanted to use for the editText
.您还可以将 styles 添加到要用于 editText 的editText
中。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.