You can use dynamic attributes to set the background color. Customize classes to implement the AttributeModifier API and set .attributeModifier() for the component for binding. The sample code is as follows:
@Entry
@Component
struct Index {
@State modifier: MyTextInputModifier = new MyTextInputModifier();
build() {
Row() {
Column() {
TextInput({ placeholder: 'test' })
.width('80%')
.height(100)
.attributeModifier(this.modifier)
}
.width('100%')
}
.height('100%')
}
}
class MyTextInputModifier implements AttributeModifier<TextInputAttribute> {
applyNormalAttribute(instance: TextInputAttribute): void {
instance.backgroundColor(Color.Grey);
}
applyPressedAttribute(instance: TextInputAttribute): void {
instance.backgroundColor(Color.Blue);
}
} Reference