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

Material TimePicker键盘模式下非活跃输入框背景色无法修改

修改Material TimePicker键盘模式非活跃输入框背景色的解决方案

要修改键盘模式下非活跃输入框的灰色背景,核心是针对键盘模式内部使用的TextInputLayout控件进行样式定制,具体步骤如下:

1. 创建自定义TextInputLayout样式

在styles.xml中定义时间选择器输入框的专属样式,指定非聚焦状态的背景色:

<style name="CustomTimePickerTextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <!-- 设置非活跃(非聚焦)状态下的输入框背景色 -->
    <item name="boxBackgroundColor">@color/your_target_color</item>
    <!-- 可选:修改边框颜色以匹配设计风格 -->
    <!-- <item name="boxStrokeColor">@color/your_stroke_color</item> -->
</style>

2. 创建TimePicker主题Overlay

继承ThemeOverlay.MaterialComponents.TimePicker,将自定义的TextInputLayout样式关联到时间选择器主题:

<style name="Timepicker_theme" parent="ThemeOverlay.MaterialComponents.TimePicker">
    <!-- 绑定自定义输入框样式到时间选择器 -->
    <item name="textInputStyle">@style/CustomTimePickerTextInputLayout</item>
</style>

3. 应用主题到MaterialTimePicker

在创建TimePicker实例时启用自定义主题:

FragmentManager fragmentManager = getSupportFragmentManager();
MaterialTimePicker materialTimePicker = new MaterialTimePicker.Builder()
        .setTimeFormat(TimeFormat.CLOCK_24H)
        .setTheme(R.style.Timepicker_theme)
        .setInputMode(MaterialTimePicker.INPUT_MODE_KEYBOARD)
        .build();
materialTimePicker.show(fragmentManager, "a");

补充说明

之前单独修改时钟模式主题无效,是因为键盘模式和时钟模式依赖不同的控件体系:时钟模式使用自定义时钟视图,而键盘模式基于Material组件的TextInputLayout,因此必须针对TextInputLayout的样式定制才能覆盖非活跃输入框的背景色。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 16:33:26