#鸿蒙通关秘籍#如何在HarmonyOS中处理不同类型的超链接交互?

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
hm673ff0ae9f335

在HarmonyOS中实现不同类型的超链接交互,可以通过自定义组件处理交互逻辑。以下是为Hashtag、Mention以及其他链接设置交互的方法:

  1. 在TextLinkSpan组件中实现超链接的交互功能,通过onClick事件响应不同类型的超链接交互。
    @Component
    struct TextLinkSpan {
      @State linkBackgroundColor: Color | Resource = Color.Transparent;
      private item: CustomSpan = new CustomSpan(CustomSpanType.Normal, '');
      @State myItem: CustomSpan = this.item;
    
      aboutToAppear(): void {
        setTimeout(() => {
          this.myItem = this.item;
        })
      }
    
      build() {
        Span(this.myItem.content)
          .fontColor($r('app.color.styled_text_link_font_color'))
          .fontSize($r('app.string.ohos_id_text_size_body1'))
          .textBackgroundStyle({ color: this.linkBackgroundColor })
          .onClick(() => {
            this.linkBackgroundColor = $r('app.color.styled_text_link_clicked_background_color');
            setTimeout(() => {
              this.linkBackgroundColor = Color.Transparent;
            }, BACKGROUND_CHANGE_DELAY);
            
            if (this.myItem.type === CustomSpanType.Hashtag) {
              promptAction.showToast({
                message: $r('app.string.styled_text_hashtag_toast_message')
              });
            } else if (this.myItem.type === CustomSpanType.Mention) {
              promptAction.showToast({
                message: $r('app.string.styled_text_user_page_toast_message')
              });
            } else {
              promptAction.showToast({
                message: $r('app.string.styled_text_content_details_toast_message')
              });
            }
          })
      }
    }
    
  2. 对于其他Span类型,如VideoLink,使用VideoLinkSpan组件,处理点击事件和显示提示信息。
    @Component
    struct VideoLinkSpan {
      @State linkBackgroundColor: Color | Resource = Color.Transparent;
      private item: CustomSpan = new CustomSpan(CustomSpanType.Normal, '');
      @State myItem: CustomSpan = this.item;
    
      aboutToAppear(): void {
        setTimeout(() => {
          this.myItem = this.item;
        })
      }
    
      build() {
        ContainerSpan() {
          ImageSpan($r('app.media.styled_text_ic_public_video'))
            .height($r('app.integer.styled_text_video_link_icon_height'))
            .verticalAlign(ImageSpanAlignment.CENTER)
          Span(this.myItem.content)
            .fontColor($r('app.color.styled_text_link_font_color'))
            .fontSize($r('app.string.ohos_id_text_size_body1'))
            .onClick(() => {
              this.linkBackgroundColor = $r('app.color.styled_text_link_clicked_background_color');
              setTimeout(() => {
                this.linkBackgroundColor = Color.Transparent;
              }, BACKGROUND_CHANGE_DELAY);
              promptAction.showToast({
                message: $r('app.string.styled_text_video_function_message')
              });
            })
        }
        .textBackgroundStyle({ color: this.linkBackgroundColor })
      }
    }
    
分享
微博
QQ
微信
回复
1天前
相关问题