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

如何修改TextInputEditText选中模式的紫色颜色?

提问内容

TextInputLayout选中状态紫色效果

我正在使用TextInputLayout和TextInputEditText控件,无法找到修改该紫色选中颜色的方法,相关资料也较少,请问该如何修改?


解决方法

方法1:全局主题统一修改

在App的主题样式中调整核心颜色属性,可一次性修改所有同类型控件的选中样式:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- 替换为你需要的目标颜色 -->
    <item name="colorPrimary">#FF5722</item>
    <item name="colorAccent">#FF5722</item>
    <!-- Material Components专属属性,控制轮廓/下划线激活色 -->
    <item name="boxStrokeColor">@color/your_target_color</item>
</style>

方法2:单个控件自定义样式

仅针对特定控件修改,不影响全局其他控件:

  1. 在styles.xml中创建自定义样式:
<style name="CustomTILStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <!-- 选中时的轮廓/下划线颜色 -->
    <item name="boxStrokeColor">@color/your_target_color</item>
    <!-- 选中时的提示文本颜色 -->
    <item name="hintTextColor">@color/your_target_color</item>
    <!-- 光标激活颜色 -->
    <item name="colorControlActivated">@color/your_target_color</item>
</style>
  1. 在布局文件中引用该样式:
<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/CustomTILStyle">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入内容"/>

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

方法3:代码动态修改

如果需要在运行时动态调整颜色,可使用以下代码:

// 修改TextInputLayout的选中轮廓颜色
textInputLayout.boxStrokeColor = ContextCompat.getColor(this, R.color.your_target_color)
// 修改提示文本的选中颜色
textInputLayout.setHintTextColor(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.your_target_color)))
// 修改TextInputEditText的光标颜色
textInputEditText.setTextCursorDrawable(R.drawable.custom_cursor)

其中custom_cursor.xml需放在drawable目录下:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:width="2dp"/>
    <solid android:color="@color/your_target_color"/>
</shape>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 10:27:55