HarmonyOS GridItem里面的组件Text无法改变文字颜色

在自定义安全键盘的时候,GridItem里的text按压时无法改变文字颜色。给text添加onTouch事件,去监听down,改变变量item入参也无法反应到ui上。

HarmonyOS
2024-12-25 12:20:17
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

当前监听不到item.isPress的变化,可以尝试以下方式,参考示例如下:

import { EKeyType, EKeyboardType, IKeyAttribute } from '../model/Constants';

@Component
export struct CustomKeyboard {
  @Prop items: IKeyAttribute[];
  @Prop curKeyboardType: EKeyboardType;
  @Link inputValue: string;
  private controller: TextInputController = new TextInputController();
  private onKeyboardEvent: Function | null = null;
  private rowSpace: number = 5; // 行间距
  private rowCount: number = 4; // 行数
  private columnSpace: number = 5; // 列间距
  private itemHeight: number = 42; // Item尺寸
  @State activeItem: string = '' // 定义一个变量来接收当前按压键

  @Builder
  myGridItem(item: IKeyAttribute, id: string) {
    Column() {
      if (typeof item.label === 'object') {
        Image(item.label)
          .width($r("app.integer.customsafekeyboard_key_image_size"))
          .height($r("app.integer.customsafekeyboard_key_image_size"))
          .objectFit(ImageFit.Contain)
      } else {
        Text(item.label)
          .fontSize(item.fontSize)
          .fontColor(this.activeItem === id ? Color.Yellow : Color.White)// 判断当前键是否是按压键,调整颜色状态
          .fontWeight(FontWeight.Bold)
      }
    }
    .width('100%')
    .height('100%')
    .onTouch((event: TouchEvent) => {
      if (event.type == TouchType.Down) {
        this.activeItem = id
      } else if (event.type == TouchType.Up) {
        this.activeItem = ''
      }
      console.log('active:::' + this.activeItem)
    })
    .justifyContent(FlexAlign.Center)

  }

  @Builder
  titleBar() {
    Stack() {
      Text($r("app.string.customsafekeyboard_keyboard_title"))
        .fontSize($r("app.integer.customsafekeyboard_keyboard_title_font_size"))
        .fontColor(Color.Grey)

      Row() {
        Text($r("app.string.customsafekeyboard_keyboard_submit"))
          .fontSize($r("app.integer.customsafekeyboard_keyboard_title_font_size"))
          .fontColor(Color.Blue)
          .onClick(() => {
            this.controller.stopEditing();
          })
      }
      .width($r("app.string.customsafekeyboard_one_hundred_percent"))
      .justifyContent(FlexAlign.End)
    }
    .width($r("app.string.customsafekeyboard_one_hundred_percent"))
    .margin({
      top: $r("app.integer.customsafekeyboard_keyboard_title_margin_top"),
      bottom: $r("app.integer.customsafekeyboard_keyboard_title_margin_bottom")
    })
  }

  build() {
    Column() {
      this.titleBar();

      Grid() {
        // 性能知识点:此处数据项较少,一屏内可以展示所有数据项,使用了ForEach。在数据项多的列表git滚动场景下,推荐使用LazyForEach。
        ForEach(this.items, (item: IKeyAttribute, index: number) => {
          GridItem() {
            this.myGridItem(item, 'key' + index)
          }
          .width($r("app.string.customsafekeyboard_one_hundred_percent"))
          .height(this.itemHeight)
          .rowStart(item?.position?.[0])
          .columnEnd(item?.position?.[1])
          .columnStart(item?.position?.[2])
          .columnEnd(item?.position?.[3])
          .backgroundColor(item.backgroundColor)
          .borderRadius($r("app.integer.customsafekeyboard_keyboard_radius"))
          .onClick(() => {
            this.onKeyboardEvent?.(item);
            if (item.type === EKeyType.CAPSLOCK && typeof item.label === 'object') {
              if (this.curKeyboardType === EKeyboardType.LOWERCASE) {
                item.label = $r("app.media.customsafekeyboard_capslock_white");
              } else {
                item.label = $r("app.media.customsafekeyboard_capslock_black");
              }
            }
          })
        }, (item: IKeyAttribute, index: number) => JSON.stringify(item) + index)
      }
      .margin({ bottom: $r("app.integer.customsafekeyboard_keyboard_marin_bottom") })
      .columnsTemplate(this.curKeyboardType === EKeyboardType.NUMERIC ? "1fr 1fr 1fr" :
        "1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr")
      .rowsTemplate("1fr 1fr 1fr 1fr") // Grid高度均分成4份
      .rowsGap(this.rowSpace) // 设置行间距
      .columnsGap(this.columnSpace) // 设置列间距
      .width($r("app.string.customsafekeyboard_one_hundred_percent"))
      .height(this.itemHeight * this.rowCount + this.rowSpace * (this.rowCount - 1))
    }
    .width($r("app.string.customsafekeyboard_one_hundred_percent"))
    .padding({ left: this.columnSpace, right: this.columnSpace })
    .backgroundColor(Color.Black)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
分享
微博
QQ
微信
回复
2024-12-25 15:01:58
相关问题
button 字体颜色无法改变
4529浏览 • 1回复 待解决
text怎么更改部分文字颜色
8022浏览 • 1回复 待解决
主工程要怎么使用module里面的颜色
654浏览 • 1回复 待解决
HarmonyOS Text组件如何设置文字方向
668浏览 • 1回复 待解决
怎么让text文本排在image里面的右下角
1332浏览 • 1回复 待解决
HarmonyOS 图片颜色改变API
619浏览 • 1回复 待解决
HarmonyOS Text组件是否支持文字描边
853浏览 • 1回复 待解决
在XComponent组件中如何改变背景颜色
1054浏览 • 1回复 待解决
哪个属性可以改变Text组件字体的粗细
2417浏览 • 1回复 待解决
HarmonyOS 如何改变图标icon颜色
686浏览 • 1回复 待解决
HarmonyOS svg改变描边颜色
689浏览 • 1回复 待解决
如何获取Text组件文字的宽度
3135浏览 • 1回复 待解决