HarmonyOS基础组件的使用

news/2024/7/21 11:36:21 标签: harmonyos, 基础组件

ArkTS是HarmonyOS优选的主力应用开发语言。它在TypeScript(简称TS)的基础上,匹配ArkUI框架,扩展了声明式UI、状态管理等相应的能力,让开发者以更简洁、更自然的方式开发跨端应用。

HarmonyOS基础组件和Compose组件很相似,看过Compose组件的话再学习HarmonyOS基础组件会感觉非常熟悉。

HarmonyOS基础组件使用

Text

Text(this.src)
  .width('100%')
  .textAlign(TextAlign.Center)
  .padding(10)
    //背景颜色
  .backgroundColor(Color.Green)
    //字体颜色
  .fontColor(Color.White)
    //设置文本装饰线
  .decoration({
    //TextDecorationType.LineThrough:穿过文本的修饰线。
    //TextDecorationType.Underline:文字下划线修饰。
    type: TextDecorationType.Underline,
    color: Color.White
  })
    //设置文本超长显示
  .textOverflow({
    //末尾省略号显示
    overflow: TextOverflow.Ellipsis
  })
    //最大行数
  .maxLines(1)
  .onClick(() => {
    //返回上个页面
    // router.back()
    //返回到指定页面并带回参数
    router.back({
      url: 'pages/Index',
      params: {
        src: 'Second页面传来的数据'
      }
    });
  })

返回上个页面用router.back()

Image

Image($r("app.media.a"))
  //ImageFit.Auto:自适应显示。
  //ImageFit.Contain:保持宽高比进行缩小或者放大,使得图片完全显示在显示边界内。
  //ImageFit.Cover:默认值,保持宽高比进行缩小或者放大,使得图片两边都大于或等于显示边界。
  //ImageFit.Fill:不保持宽高比进行放大缩小,使得图片充满显示边界。
  //ImageFit.None:保持原有尺寸显示。
  //ImageFit.ScaleDown:保持宽高比显示,图片缩小或者保持不变。
  .objectFit(ImageFit.Contain)
  .width('100%')
  .height(200)
  .margin(5)

//Image加载网络图片
Image('https://lmg.jj20.com/up/allimg/4k/s/02/210924233115O14-0-lp.jpg')
  .width('100%')

引用的图片文件目录

网络权限添加在module.json5中

{
  "module": {
    "name": "entry",
    "type": "entry",
    "description": "$string:module_desc",
    "mainElement": "EntryAbility",
    "deviceTypes": [
      "phone",
      "tablet"
    ],
    "deliveryWithInstall": true,
    "installationFree": false,
    "pages": "$profile:main_pages",
    "abilities": [
      {
        "name": "EntryAbility",
        "srcEntry": "./ets/entryability/EntryAbility.ts",
        "description": "$string:EntryAbility_desc",
        "icon": "$media:icon",
        "label": "$string:EntryAbility_label",
        "startWindowIcon": "$media:icon",
        "startWindowBackground": "$color:start_window_background",
        "exported": true,
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ]
      }
    ],
    "requestPermissions": [
      {
        //网络访问权限
        "name": "ohos.permission.INTERNET"
      }
    ]
  }
}

TextInput

TextInput({ placeholder: '请输入账号' })
  //提示文本颜色
  .placeholderColor(0x999999)
    //提示文本大小及样式设置
  .placeholderFont({
    size: 20,
    weight: FontWeight.Medium,
    family: 'cursive',
    style: FontStyle.Italic
  })
  .fontColor(Color.Blue)
  .fontSize(20)
  .fontFamily('Arial')
  .margin(10)
    //输入类型设置
  .type(InputType.Password)
    //获取输入文本
  .onChange((value: string) => {
    this.text = value
  })

Button

//Button
Button($r('app.string.login_text'), {
  //ButtonType.Capsule:胶囊形按钮
  //ButtonType.Circle:圆形按钮
  // ButtonType.Normal:正常按钮
  type: ButtonType.Capsule,
  //是否开启点击效果
  stateEffect: true
})
  .width('90%')
  .height($r('app.float.button_height'))
  .fontSize($r('app.float.login_fontSize'))
  .fontWeight(FontWeight.Medium)
  .backgroundColor('#007DFF')
//包含子组件
Button({ type: ButtonType.Circle, stateEffect: true }) {
  Image($r('app.media.app_icon'))
    .width(30)
    .height(30)
}
.width(55)
.height(55)
.backgroundColor(0x317aff)
.margin(10)

宽高文件float.json

 

{
  "float": [
    {
      "name": "button_height",
      "value": "40vp"
    },
    {
      "name": "login_fontSize",
      "value": "18fp"
    }
  ]
}

 宽高单位用vp,字体大小用fp。

LoadingProgress

LoadingProgress()
  .color(Color.Blue)
  .height(60)
  .width(60)

路由跳转

引入

import router from '@ohos.router'

点击跳转

Text(this.message)
  .fontSize(50)
  .fontWeight(FontWeight.Bold)
  .onClick(() => {
    //pushUrl:跳转后当前页面还在
    //replaceUrl:跳转后当前页面销毁
    router.pushUrl({
      url: 'pages/Second',
      params: {
        src: 'Index页面传来的数据,Index页面传来的数据,Index页面传来的数据',
      }
    }, router.RouterMode.Single)
  })

下个页面接收

@State src: string = router.getParams()?.['src'];

完整代码:

Index.ets

import router from '@ohos.router'

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  @State src: string = '';

  onPageShow() {
    //Second页面带回的参数
    this.src = router.getParams()?.['src'];
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            //pushUrl:跳转后当前页面还在
            //replaceUrl:跳转后当前页面销毁
            router.pushUrl({
              url: 'pages/Second',
              params: {
                src: 'Index页面传来的数据,Index页面传来的数据,Index页面传来的数据',
              }
            }, router.RouterMode.Single)
          })

        Text(this.src)
      }
      .width('100%')
    }
    .height('100%')
  }
}

Second.ets

import router from '@ohos.router';

@Entry
@Component
struct TextDemo {
  @State src: string = router.getParams()?.['src'];
  @State text: string = ''

  build() {
    Column() {
      //Text
      Text(this.src)
        .width('100%')
        .textAlign(TextAlign.Center)
        .padding(10)
          //背景颜色
        .backgroundColor(Color.Green)
          //字体颜色
        .fontColor(Color.White)
          //设置文本装饰线
        .decoration({
          //TextDecorationType.LineThrough:穿过文本的修饰线。
          //TextDecorationType.Underline:文字下划线修饰。
          type: TextDecorationType.Underline,
          color: Color.White
        })
          //设置文本超长显示
        .textOverflow({
          //末尾省略号显示
          overflow: TextOverflow.Ellipsis
        })
          //最大行数
        .maxLines(1)
        .onClick(() => {
          //返回上个页面
          // router.back()
          //返回到指定页面并带回参数
          router.back({
            url: 'pages/Index',
            params: {
              src: 'Second页面传来的数据'
            }
          });
        })

      //Image加载本地图片
      Image($r("app.media.a"))
        //ImageFit.Auto:自适应显示。
        //ImageFit.Contain:保持宽高比进行缩小或者放大,使得图片完全显示在显示边界内。
        //ImageFit.Cover:默认值,保持宽高比进行缩小或者放大,使得图片两边都大于或等于显示边界。
        //ImageFit.Fill:不保持宽高比进行放大缩小,使得图片充满显示边界。
        //ImageFit.None:保持原有尺寸显示。
        //ImageFit.ScaleDown:保持宽高比显示,图片缩小或者保持不变。
        .objectFit(ImageFit.Contain)
        .width('100%')
        .height(200)
        .margin(5)

      //Image加载网络图片
      Image('https://lmg.jj20.com/up/allimg/4k/s/02/210924233115O14-0-lp.jpg')
        .width('100%')

      TextInput({ placeholder: '请输入账号' })
        //提示文本颜色
        .placeholderColor(0x999999)
          //提示文本大小及样式设置
        .placeholderFont({
          size: 20,
          weight: FontWeight.Medium,
          family: 'cursive',
          style: FontStyle.Italic
        })
        .fontColor(Color.Blue)
        .fontSize(20)
        .fontFamily('Arial')
        .margin(10)
          //输入类型设置
        .type(InputType.Password)
          //获取输入文本
        .onChange((value: string) => {
          this.text = value
        })

      Text(this.text)

      //Button
      Button($r('app.string.login_text'), {
        //ButtonType.Capsule:胶囊形按钮
        //ButtonType.Circle:圆形按钮
        // ButtonType.Normal:正常按钮
        type: ButtonType.Capsule,
        //是否开启点击效果
        stateEffect: true
      })
        .width('90%')
        .height($r('app.float.button_height'))
        .fontSize($r('app.float.login_fontSize'))
        .fontWeight(FontWeight.Medium)
        .backgroundColor('#007DFF')

      //包含子组件
      Button({ type: ButtonType.Circle, stateEffect: true }) {
        Image($r('app.media.app_icon'))
          .width(30)
          .height(30)
      }
      .width(55)
      .height(55)
      .backgroundColor(0x317aff)
      .margin(10)

      //LoadingProgress
      LoadingProgress()
        .color(Color.Blue)
        .height(60)
        .width(60)
    }
    .width('100%')
  }
}


http://www.niftyadmin.cn/n/5144586.html

相关文章

分类预测 | Matlab实现KOA-CNN-LSTM-selfAttention多特征分类预测(自注意力机制)

分类预测 | Matlab实现KOA-CNN-LSTM-selfAttention多特征分类预测(自注意力机制) 目录 分类预测 | Matlab实现KOA-CNN-LSTM-selfAttention多特征分类预测(自注意力机制)分类效果基本描述程序设计参考资料 分类效果 基本描述 1.Mat…

C++并发与多线程(14) | condition_varible、wait、notify_one、notify_all

概念 当谈到并发编程,特别是在多线程或多进程的上下文中,条件变量(Condition Variables)、等待(wait)、通知一个(notify_one)和通知所有(notify_all)是常见的概念。它们用于协调线程的执行,以实现同步并避免竞争条件。以下是对每个术语的解释: 条件变量: 条件变量…

轻松管理MySQL权限:Python脚本带你飞

数据库管理是 IT 专家和开发者日常工作中的重要组成部分。一个合适的用户权限管理系统不仅确保了数据的安全性,还能确保数据能够按照预期的方式被正确地访问和修改。在本文中,我们将探讨如何使用 Python 脚本来管理和查询 MySQL 数据库中的用户权限。 用户权限管理:创建或修…

《NTP-VFL - A New Scheme for Non-3rd Party Vertical Federated Learning》模型原理

一、概要 目前现存算法的三个局限性: 理论上的可信三方现实不存在,通常是联邦学习的主要瓶颈。通信和计算成本随着迭代次数的提升,增长很快。扩展性不好,隐私保护的最优模型在两方和多方中不兼容。 本文提出一个没有第三方的联…

设置QT应用程序的图标

前言 设置QT程序的图标以及会话框图标 方法 针对windows系统 使用RC_ICONS 在项目中的.pro文件中添加: RC_ICONS logo.ico结果 运行程序,生成的.exe文件和会话框的左上角添加logo成功。

web3之以太坊链二层(layer2):StarkNet

文章目录 web3之以太坊链二层(layer2):StarkNet什么是StarkNet背景:StarkWare、StarkEx、StartNet、STARK StarkNet生态项目StarkNet生态系统钱包Argent X官方桥StarkGateMint Square web3之以太坊链二层(layer2&#…

什么是物流RPA?物流RPA解决什么问题?物流RPA实施难点在哪里?

RPA指的是机器人流程自动化,它是一套模拟人类在计算机、平板电脑、移动设备等界面执行任务的软件。通过RPA,可以自动完成重复性、繁琐的工作,提高工作效率和质量,降低人力成本。RPA适用于各种行业和场景,例如财务、人力…

齿轮加工刀片,图书馆数字服务平台整合技术服务

序号 名称 参数 1 阅读生态服务平台 欠款支付用户在移动设备对书籍滞纳金进行查询缴纳。 用户可查看到的信息包含:欠费总金额、欠费清单(书名、逾期天数、应归还时间、超期金额、备注、实际归还时间)。 滞纳金支付方式:微信…