Click Event Bubble Prevention, Payment Input Dialog, Color Handling Methods, Text Component Positioning

[Daily HarmonyOS Next Knowledge] Click Event Bubble Prevention, Payment Input Dialog, Color Handling Methods, Text Component Positioning, Text Component Width Compression

1. How to prevent event bubbling for the HarmonyOS onClick ev…


This content originally appeared on DEV Community and was authored by kouwei qing

[Daily HarmonyOS Next Knowledge] Click Event Bubble Prevention, Payment Input Dialog, Color Handling Methods, Text Component Positioning, Text Component Width Compression

1. How to prevent event bubbling for the HarmonyOS onClick event?

How to prevent event bubbling for the onClick event?

Since only events supporting stopPropagation can use this method, and the ClickEvent for click events does not support it, you cannot directly use stopPropagation to prevent event bubbling.

2. HarmonyOS payment input dialog?

Payment input dialog

The overall implementation can use CustomDialog. Since payment inputs have no cursor, it is recommended to use six Text components spliced together instead of TextInput. When the custom keyboard is clicked, trigger an event to display the input in the Text components. Use images to display encrypted characters like *. Refer to the following implementation:

@CustomDialog
// Dialog decorator for custom dialogs
struct CustomDialogExample {
  controller: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({}),
  })
  services: Array<string> = ['1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '0', 'X']
  numbers: Array<number> = [1, 2, 3, 4, 5, 6]
  @State Inputs: Array<string> = []

  build() { // Set dialog content
    Column() {
      Text('Please enter payment password')
        .fontSize(20)
        .margin({ top: 10, bottom: 10 })
      Row({ space: 5 }) {
        ForEach(this.numbers, (item: number) => {
          Text(this.Inputs.length >= item ? '*' : '')
            .width(20)
            .height(20)
            .backgroundColor('#C0C0C0')
            .textAlign(TextAlign.Center)
        })
      }
      .width('100%')
      .height('15%')
      .justifyContent(FlexAlign.Center)

      Grid() {
        ForEach(this.services, (service: string) => {
          GridItem() {
            Text(service)
          }
          .borderWidth(0.4)
          .borderColor(Color.Gray)
          .onClick(() => {
            if (service != 'X' && service != ' ') {
              this.Inputs.push(service)
              if (this.Inputs.length == 6) {
                this.controller.close()
              }
            }
            if (service == 'X') {
              this.Inputs.pop()
            }
          })
        })
      }
      .width('100%')
      .height('60%')
      .rowsTemplate('1fr 1fr 1fr 1fr')
      .columnsTemplate('1fr 1fr 1fr')
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.SpaceBetween)
  }
}

@Entry
@Component
struct Index {
  dialogController: CustomDialogController = new CustomDialogController({
    // Dialog constructor, corresponding to the decorator
    builder: CustomDialogExample(),
  })

  build() {
    Column() {
      Button('Pay')
        .onClick(() => {
          this.dialogController.open()
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.SpaceAround)
  }
}

3. HarmonyOS method to get ARGB of a color or handle colors?

uint32_t OH_Drawing_BrushGetColor (const OH_Drawing_Brush * )

Gets the color attribute of a brush. The color attribute describes the color used when the brush fills a graphic, represented by a 32-bit (ARGB) variable. For details, refer to the official documentation: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/drawing__brush_8h-V5

To get color values, you can also use getColorSync(resId: number) : number; to retrieve the color value corresponding to a specified resource ID synchronously. Detailed documentation: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-resource-manager-V5#getcolorsync10

getColorSync(resId: number) : number;

Retrieves the color value corresponding to a specified resource ID synchronously.

For other color types, refer to: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-types-V5#resourcecolor

ResourceColor

Color type used to describe resource colors.

Type Description
Color Color enum value.
number HEX format color, supporting rgb or argb. Examples: 0xffffff, 0xffff0000. The number type cannot识别 the number of digits passed; the format is determined by the value size (e.g., 0x00ffffff is parsed as rgb).
string rgb or argb format color. Examples: '#ffffff', '#ff000000', 'rgb(255, 100, 255)', 'rgba(255, 100, 255, 0.5)'.
Resource Introduces colors from system resources or application resources using resource references.

4. How to set the position of a HarmonyOS Text component to the upper right corner?

The Edges type supports right and left, allowing positioning in the upper right corner. Refer to the following demo:

@Entry
@Component
struct Index {
  build() {
    Column() {
      Text('66666')
        .width(100)
        .height(50)
        .backgroundColor('#f04b3d')
        .borderRadius({
          bottomLeft: 12
        })
        .textAlign(TextAlign.Center)
        .fontSize(10)
      Flex().width(10).height(10).backgroundColor(Color.Blue).position({right:0})
    }
    .margin(50)
  }
}

5. When there are multiple Text components in a Flex in HarmonyOS, how to compress the width of a specific Text?

Use the flexShrink property in combination with textOverflow, wordBreak, and maxLines to compress the width and content of a Text component:

@Entry
@Component
struct AdaptiveText {
  build() {
    Flex({ direction: FlexDirection.Row }) {
      Text('flexShrink(2)flexShrink(2)flexShrink(2)')
        .flexShrink(2)
        .width(200)
        .height(100)
        .backgroundColor(0xF5DEB3)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .wordBreak(WordBreak.BREAK_WORD)
        .maxLines(2)

      Text('no flexShrink')
        .width(200)
        .height(100)
        .backgroundColor(0xD2B48C)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .wordBreak(WordBreak.BREAK_WORD)
        .maxLines(2)

      Text('flexShrink(6)flexShrink(6)flexShrink(6)')
        .flexShrink(6)
        .width(200)
        .height(100)
        .backgroundColor(0xF5DEB3)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .wordBreak(WordBreak.BREAK_WORD)
        .maxLines(2)
    }.width("100%").height(120).padding(10).backgroundColor(0xAFEEEE)
  }
}


This content originally appeared on DEV Community and was authored by kouwei qing


Print Share Comment Cite Upload Translate Updates
APA

kouwei qing | Sciencx (2025-06-28T12:15:42+00:00) Click Event Bubble Prevention, Payment Input Dialog, Color Handling Methods, Text Component Positioning. Retrieved from https://www.scien.cx/2025/06/28/click-event-bubble-prevention-payment-input-dialog-color-handling-methods-text-component-positioning/

MLA
" » Click Event Bubble Prevention, Payment Input Dialog, Color Handling Methods, Text Component Positioning." kouwei qing | Sciencx - Saturday June 28, 2025, https://www.scien.cx/2025/06/28/click-event-bubble-prevention-payment-input-dialog-color-handling-methods-text-component-positioning/
HARVARD
kouwei qing | Sciencx Saturday June 28, 2025 » Click Event Bubble Prevention, Payment Input Dialog, Color Handling Methods, Text Component Positioning., viewed ,<https://www.scien.cx/2025/06/28/click-event-bubble-prevention-payment-input-dialog-color-handling-methods-text-component-positioning/>
VANCOUVER
kouwei qing | Sciencx - » Click Event Bubble Prevention, Payment Input Dialog, Color Handling Methods, Text Component Positioning. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/06/28/click-event-bubble-prevention-payment-input-dialog-color-handling-methods-text-component-positioning/
CHICAGO
" » Click Event Bubble Prevention, Payment Input Dialog, Color Handling Methods, Text Component Positioning." kouwei qing | Sciencx - Accessed . https://www.scien.cx/2025/06/28/click-event-bubble-prevention-payment-input-dialog-color-handling-methods-text-component-positioning/
IEEE
" » Click Event Bubble Prevention, Payment Input Dialog, Color Handling Methods, Text Component Positioning." kouwei qing | Sciencx [Online]. Available: https://www.scien.cx/2025/06/28/click-event-bubble-prevention-payment-input-dialog-color-handling-methods-text-component-positioning/. [Accessed: ]
rf:citation
» Click Event Bubble Prevention, Payment Input Dialog, Color Handling Methods, Text Component Positioning | kouwei qing | Sciencx | https://www.scien.cx/2025/06/28/click-event-bubble-prevention-payment-input-dialog-color-handling-methods-text-component-positioning/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.