You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改TextInputEditText(FilledBox样式)的提示文字样式

实现TextInputEditText的FilledBox样式及动态提示文字样式

要实现你需要的效果,核心是利用Material Design的TextInputLayout包裹TextInputEditText,并通过自定义样式分别控制输入前后提示文字的外观,具体步骤如下:

1. 确保依赖Material组件

在模块级build.gradle(或build.gradle.kts)中添加Material组件依赖:

dependencies {
    implementation 'com.google.android.material:material:1.11.0' // 可替换为最新稳定版本
}

2. 自定义提示文字样式

在res/values/styles.xml中定义两个样式,分别对应输入前后的提示文字状态:

<!-- 未输入内容时的hint样式:粗体红色 -->
<style name="UnfilledHintStyle">
    <item name="android:textColor">#FF0000</item>
    <item name="android:textStyle">bold</item>
</style>

<!-- 输入内容后的浮动label样式:常规黑色 -->
<style name="FilledFloatingLabelStyle">
    <item name="android:textColor">#000000</item>
    <item name="android:textStyle">normal</item>
</style>

3. 布局中配置组件

使用TextInputLayout包裹TextInputEditText,设置FilledBox样式,并关联自定义的提示样式:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
    app:hintTextAppearance="@style/UnfilledHintStyle"
    app:floatingLabelTextAppearance="@style/FilledFloatingLabelStyle"
    android:hint="请输入你的内容">

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

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

注意事项

  • 页面必须使用Material主题(如Theme.MaterialComponents.DayNight.NoActionBar),否则TextInputLayout的FilledBox样式无法正常生效。
  • 若需调整FilledBox的背景填充色,可在TextInputLayout的样式中添加<item name="boxBackgroundColor">@color/your_custom_color</item>自定义。

内容的提问来源于stack exchange,提问作者Juro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 16:42:39