【发布时间】:2021-04-13 19:45:05
【问题描述】:
我想在 Material Design 的 TextInputLayout 填充文本中制作底线的圆角。我可以在聚焦和不聚焦模式下更改它的颜色,但是如何将它的角更改为圆形。 这就是我现在拥有的-
我正在寻找以下结果-
这是我尝试但没有做太多的代码 -
<com.google.android.material.textfield.TextInputLayout
app:shapeAppearanceOverlay="@style/Rounded_ShapeAppearanceOverlay.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
app:boxStrokeColor="@color/selector_otp_input_box_stroke_color"
app:boxStrokeWidth="6dp"
app:boxCornerRadiusBottomEnd="5dp"
app:boxCornerRadiusBottomStart="5dp"
app:boxCornerRadiusTopEnd="5dp"
app:boxCollapsedPaddingTop="5dp"
app:boxStrokeWidthFocused="6dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:digits="1234567890"
android:inputType="number"
android:textSize="40sp"
android:textIsSelectable="false"
android:maxLength="1"
android:maxLines="1"
android:backgroundTint="@color/colorPrimary"/>
</com.google.android.material.textfield.TextInputLayout>
样式 Rounded_ShapeAppearanceOverlay
<style name="Rounded_ShapeAppearanceOverlay.MaterialComponents.TextInputLayout.FilledBox"
parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="cornerSizeBottomLeft">4dp</item>
<item name="cornerSizeBottomRight">4dp</item>
</style>
任何工作将不胜感激,谢谢
【问题讨论】:
-
你是说在上面的设计中每个盒子都是
TextInputLayout吗?所以你有 6 个 TextInputLayout 用于这个设计? -
@ADM 是的,你是对的,我试图创建 OTP 字段,每个焦点都会改变底线的颜色,所以我采用了这个实现,除了底线的圆角之外,一切都很完美
-
用 6
EditText实现这可能非常棘手,因为您每次都必须处理焦点更改,而且您还必须处理文本删除,简而言之,这可能会导致错误。这应该用一个单一的编辑文本来实现所以你最好自定义EditText以这种方式工作。要开始使用,只需搜索PinEntryEditText你就会明白。 -
@ADM 我之前尝试过使用不同的方法,就像你用一个 editText 说的那样,现在用上面的一种方法和一个 EditText 上的 TextWatcher 解决,我可以用更深入的方法再试一次,只用一个 EditText 但它会如果有任何选项我可以用圆角在 TextInputLayout 下划线,那就太好了...
标签: android android-layout material-components-android android-textinputlayout android-textinputedittext