如何让TextInputLayout可点击且实现正常的Ripple水波纹效果
解决方案
方案1:使用官方ExposedDropdownMenu样式(最稳定,对齐Material规范)
这个方案不需要手动实现水波纹,官方样式已经内置了和ExposedDropdownMenu完全一致的水波纹效果,同时可以保留原有OutlinedBox的外观,不需要显示下拉箭头,是官方推荐的可点击文本输入框实现方案。
修改后的XML代码:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/tilPlannedStartTime" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/planned_start_time" android:padding="8dp" app:endIconMode="none"> <!-- 隐藏默认的下拉箭头,保持原有外观 --> <com.google.android.material.textfield.MaterialAutoCompleteTextView android:id="@+id/tielPlannedStartTime" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="none" android:clickable="false" android:focusable="false" android:singleLine="true" /> </com.google.android.material.textfield.TextInputLayout>
逻辑代码调整:
只需要给TextInputLayout设置点击事件即可,不需要给内部的TextView重复设置,避免事件冲突:
binding.tielPlannedStartTime.setText(workout.item.plannedStartTime?.toString(SimpleTime.Format.DisplayHM) ?: "-") binding.tilPlannedStartTime.setOnClickListener { // 在这里写打开对话框的逻辑即可 Toast.makeText(it.context, "点击触发", Toast.LENGTH_SHORT).show() }
方案2:手动添加水波纹效果(不改原有控件结构)
如果你不想更换为ExposedDropdownMenu样式,可以直接给TextInputLayout添加水波纹前景,保留原有所有控件属性:
仅需要给TextInputLayout增加2个属性:
<com.google.android.material.textfield.TextInputLayout ... android:clickable="true" android:foreground="?attr/selectableItemBackground"> <!-- 原有内部EditText代码保持不变 --> </com.google.android.material.textfield.TextInputLayout>
逻辑代码和方案1一致,仅给TextInputLayout设置点击事件即可。
原问题原因说明
之前的实现没有水波纹的原因是:
- 点击事件被内部的
TextInputEditText拦截,但EditText没有设置水波纹背景 - 外层
TextInputLayout默认没有绑定点击对应的水波纹drawable,所以点击时没有视觉反馈
内容的提问来源于stack exchange,提问作者prom85
相关产品推荐
相关产品推荐

