《跟我一起学“Harmony-ArkTS”》——评分条组件和弹窗组件

一、评分条组件(Rating)

评分条组件是常用于收集用户反馈的UI元素,它允许用户通过点击来选择分数。
代码案例如下:

@Entry
@Component
struct RatingPage {
  @State rating: number = 0;
  private title: string = '评分条案例';

  build() {
    Column({ space: 10 }) {
      Text(this.title)
        .fontSize(20)
        .fontWeight(FontWeight.Bold)

      Rating({
        rating: this.rating,
        indicator: false
      })
        .width(300)
        .height(50)
        .stars(5)
        .stepSize(0.1)
        .onChange((rating) => {
          this.rating = rating;
        })

      Text(`当前评分:${this.rating}`)
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .textAlign(TextAlign.Center)
    }
    .height('100%')
    .width('100%')
  }
}

以上就是评分条组件的使用方法,更多关于评分条组件使用方法的内容,大家可以参考Rating评分条组件官方文档进行深入学习。

二、弹窗组件

弹窗组件有很多种,本案例只介绍最常用的两种弹窗组件,AlertDialog组件和自定义组件。

  1. AlertDialog组件
    AlertDialog组件是操作确认类弹出框,触发一个将产生严重后果的不可逆操作时,如删除、重置、取消编辑、停止等。
    代码案例如下:
@Entry
@Component
struct AlertDialogPage {
  private title: string = '弹窗组件案例';

  build() {
    Column({ space: 20 }) {
      Text(this.title)
        .fontSize(20)
        .fontWeight(FontWeight.Bold)

      Button('AlertDialog组件')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          AlertDialog.show({
            title: '我是标题',
            subtitle: '我是副标题',
            message: '我是提示内容'
          })
        })

      Button('模拟删除对话空')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          AlertDialog.show({
            title: '提示',
            message: '数据删除后不可恢复,确认删除本条数据?',
            confirm: {
              value: '确定',
              action: () => {
                AlertDialog.show({
                  title: '提示',
                  message: '删除成功'
                });
              }
            }
          })
        })
    }
    .height('100%')
    .width('100%')
  }
}

以上就是AlertDialog组件的使用方法,更多关于AlertDialog组件使用方法的内容,大家可以参考AlertDialog组件官方文档进行深入学习。

  1. 自定义弹窗组件
    自定义弹窗组件因为更加灵活,因此适用于各种场景,包括但不限于:广告展示、中奖通知、警告信息、软件更新提示等。
    @CustomDialog装饰器用于装饰自定义弹窗组件,可以在此装饰器内进行自定义弹窗的内容。
    代码案例如下:
@Entry
@Component
struct MyDialogPage {
  private title: string = '自定义弹窗组件案例';
  controller: CustomDialogController = new CustomDialogController({
    builder: MyDialog({
      cancel: () => {
        this.onCancel();
      },
      confirm: () => {
        this.onConfirm();
      }
    }),
    alignment: DialogAlignment.Center // 设置弹窗组件的对齐方式
  });

  onCancel() {
    console.log('cancel回调');
  }

  onConfirm() {
    console.log('confirm回调');
  }

  build() {
    Column({ space: 20 }) {
      Text(this.title)
        .fontSize(20)
        .fontWeight(FontWeight.Bold)

      Button('自定义弹窗')
        .onClick(() => {
          this.controller.open();
        })
    }
    .height('100%')
    .width('100%')
  }
}

/**
 * 自定义弹窗组件
 */
@CustomDialog
struct MyDialog {
  controller: CustomDialogController;
  cancel: Function = () => {};
  confirm: Function = () => {};

  build() {
    Column({space: 20}) {
      Text('我是自定义弹窗')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)

      Button('cancel')
        .onClick(() => {
          this.controller.close();
          this.cancel();
        })

      Button('confirm')
        .onClick(() => {
          this.controller.close();
          this.confirm();
        })
    }
    .width('100%')
    .height('100%')
  }
}

三、小结

本章言简意赅的为大家介绍了评分条组件(Rating)和弹窗组件(Dialog),下一章,为大家介绍MVVM(模型-视图-视图模型)的内容。最后,创作不易,如果大家觉得我的文章对学习鸿蒙有帮助的话,就动动小手,点个免费的赞吧!收到的赞越多,我的创作动力也会越大哦,谢谢大家🌹🌹🌹!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学徒钝子生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值