04.HarmonyOS-组件使用

1.权限申请

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V2/accesstoken-guidelines-0000001493744016-V2

权限字段说明,系统授权可以只填名称,如果是user_grant需要填写更多信息


权限列表,可以查看是系统受限还是用户授权:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V2/permission-list-0000001544464017-V2

这个为系统授权


这个为用户授权


权限设置


2.图片


网络图片使用


本地资源加载


3.文本显示


多语言文件调用



4.文本输入框



5.按钮



6.滑动条组件



7.布局


行列容器


justifyContent=主轴控制,alignItems=交叉轴控制(Row容器使用VerticalAlign枚举,Column使用HorizontaLalign)。



主轴布局


交叉轴布局


8.实例

@Entry
@Component
struct Index {

  //监听改变的变量
  @State imageWidth: number = 200

  build() {
    Column() {
      Row() {
        //网络图片,需要访问网络权限
        Image($r("app.media.icon"))
          .width(this.imageWidth)
          .interpolation(ImageInterpolation.High)
      }
      .width("100%")
      .height("50%")
      .justifyContent(FlexAlign.Center);

      Row() {
        //显示文本
        Text($r("app.string.image_width"))
          .width(100)
          .backgroundColor("#fff")
          .fontSize(20)

        //输入框
        //数字转字符串:.toFixed(0)
        TextInput({ placeholder: "输入图片宽度", text: this.imageWidth.toFixed(0) })
          .width(100)
          .backgroundColor("#fff")
          .fontSize(20)
          .type(InputType.Number)
          .onChange((value) => {
            if (value.length > 0) {
              //字符串转数字:parseInt(value)
              this.imageWidth = parseInt(value)
            } else {
              this.imageWidth = 0
            }
          })
          .textAlign(TextAlign.End)
      }
      .width("100%")
      .padding({ left: 10, right: 10 })
      .justifyContent(FlexAlign.SpaceBetween)

      Divider()
        .padding({ left: 10, right: 10 });


      Row() {
        Button("放大").onClick((event: ClickEvent) => {
          this.imageWidth += 10;
        })
        Button("缩小").onClick((event: ClickEvent) => {
          this.imageWidth -= 10;
        })
      }
      .width("100%")
      .margin({top:20,bottom:20})
      .justifyContent(FlexAlign.SpaceAround)



      Row(){
        Slider({
          min: 0,
          max: 100,
          value: this.imageWidth / 10,
          step: 10
        })
          .width("100%")
          .height(20)
          .blockColor("#0f0")
          .trackThickness(20)
          .showTips(true)
          .onChange((value, mode) => {
            this.imageWidth = value * 10;
          });
      }




    }
    .width('100%')
  }
}












(1)