HarmonyOS鸿蒙Next中TextArea设置属性enabled为false时,会改变字体的颜色。

HarmonyOS鸿蒙Next中TextArea设置属性enabled为false时,会改变字体的颜色。 TextArea设置属性enabled为false时,会改变字体的颜色,变成灰色字体。但实际不满足业务需求。

3 回复

enabled属性为false,fontColor属性就失效了。enable=false的样式,是UX的标准规格,无法更改。

可以通过设置focusable属性让组件是否获焦,来控制是否禁用出入。

demo如下:

@Entry
@Component
struct TextAreaExample {
  @State text: string = '哈哈哈'
  @State positionInfo: CaretOffset = { index: 0, x: 0, y: 0 }
  controller: TextAreaController = new TextAreaController()
  @State flag: boolean = true;

  build() {
    Column() {
      TextArea({
        text: this.text,
        placeholder: 'The text area can hold an unlimited amount of text. input your word...',
        controller: this.controller
      })//通过控制是否失去焦点,使禁止输入
        .focusable(this.flag)
        .placeholderFont({ size: 16, weight: 400 })
        .width(336)
        .height(56)
        .margin(20)
        .fontSize(16)//请使用十六进制代码设置颜色
          // .fontColor('#ff9900')
        .fontColor(Color.Orange)//避免使用系统内置的颜色定义颜色,除了黑色意外的其他颜色依旧会被置灰
        .backgroundColor('#FFFFFF')
        .onChange(value => {
          this.text = value
        })
      Text(this.text)
        .focusable(this.flag)
      Button('Set caretPosition 1')
        .backgroundColor('#007DFF')
        .margin(15)
        .onClick(() => {
          // 设置光标位置到第一个字符后
          this.controller.caretPosition(1)
        })
        .focusable(false)
      Button('Get CaretOffset')
        .backgroundColor('#007DFF')
        .margin(15)
        .onClick(() => {
          this.positionInfo = this.controller.getCaretOffset()
        })
        .focusable(this.flag)
      Button('禁用或启动flag')
        .backgroundColor('#007DFF')
        .margin(15)
        .onClick(() => {
          console.info("flag前:" + this.flag)
          this.flag == true ? this.flag = false : this.flag = true
          console.info("flag后:" + this.flag)
        })
    }.width('100%').height('100%').backgroundColor('#F1F3F5')
  }
}

更多关于HarmonyOS鸿蒙Next中TextArea设置属性enabled为false时,会改变字体的颜色。的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,当TextArea组件的enabled属性设置为false时,字体的颜色会自动变为灰色,以表示该组件当前不可用。这种视觉反馈是系统默认的行为,旨在提示用户该输入区域不可编辑。开发者可以通过自定义样式或使用其他属性来覆盖默认的颜色变化。

在HarmonyOS鸿蒙Next中,当TextArea的enabled属性设置为false时,控件会进入禁用状态,此时系统会自动将字体颜色改为灰色,以直观地提示用户该控件不可编辑或不可交互。这是为了遵循用户体验设计规范,确保用户能够清晰区分可用与不可用的控件状态。如果你希望自定义禁用状态下的字体颜色,可以通过样式或代码手动设置。

回到顶部