HarmonyOS | UI开发 (一) | 基础组件(Text/Span,TextInput/TextArea,Button,Image)


个人博客主页谭祖爱 & 技术博客

项目实例地址RecordHarmonyOSProject


前言

鸿蒙操作系统作为华为推出的全新分布式操作系统,为开发者提供了丰富的组件库,使得开发者能够快速构建功能强大、界面美观的应用程序。本文将深入探讨鸿蒙应用开发中常用的组件,帮助开发者更好地理解和应用这些组件,提升开发效率和用户体验。


一、基础组件

基础组件:Text/Span,TextInput/TextArea,Button,Image


1.1.Text的概述

Text是文本组件,通常用于展示用户的视图,如显示文章的文字

1.2.Text的创建方式

Text创建方式:1.string字符串,2.引用Resource资源

- string字符串创建

Text('我是一段文本')

- 引用Resource资源

通过 $r('app.string.xxx') 引用 string.json 文件中的 xxx(属性名) 对于的值

Text($r('app.string.app_name'))
        .baselineOffset(0)
        .fontSize(30)
        .border({ width: 1 })
        .padding(10)
        .width(300)

1.3.Text自定义文本样式

- 通过textAlign属性设置文本对齐样式

Text('左对齐')
  .width(300)
  .textAlign(TextAlign.Start)

Text('中间对齐')
  .width(300)
  .textAlign(TextAlign.Center)

Text('右对齐')
  .width(300)
  .textAlign(TextAlign.End)

- 通过textOverflow属性控制文本超长处理

Text('This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content. This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content.')
          .width(250)
          .textOverflow({overflow:TextOverflow.None})
          .maxLines(1)
          .fontSize(12)
          .border({width:1}).padding(10)

Text('This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content. This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content.')
          .width(250)
          .textOverflow({overflow:TextOverflow.Ellipsis})
          .maxLines(1)
          .fontSize(12)
          .border({width:1}).padding(10)

- 通过lineHeight属性设置文本行高

Text('This is the text with the line height set. This is the text with the line height set.')
  .width(300).fontSize(12).border({ width: 1 }).padding(10)
Text('This is the text with the line height set. This is the text with the line height set.')
  .width(300).fontSize(12).border({ width: 1 }).padding(10)
  .lineHeight(20)

- 通过copyOption属性设置文本是否可复制粘贴

Text("这是一段可复制文本")
  .fontSize(30)
  .copyOption(CopyOptions.InApp)

1.4.Text添加事件

Text('点我')
.onClick(()=>{
    console.info('我是Text的点击响应事件');
 })

2.1.Span的概述

Span只能作为Text组件的子组件显示文本内容。可以在一个Text内添加多个Span来显示一段信息

2.2.创建Span

Text('我是Text') {
    Span('我是Span')
}
.padding(10)
.borderWidth(1)

- Span设置文本装饰线及颜色

Text() {
       Span('我是Span1,').fontSize(16).fontColor(Color.Black)
         .decoration({type:TextDecorationType.None})
       Span('我是Span1,').fontSize(16).fontColor(Color.Grey)
         .decoration({ type: TextDecorationType.LineThrough, color: Color.Red })
       Span('我是Span2').fontColor(Color.Blue).fontSize(16)
         .fontStyle(FontStyle.Italic)
         .decoration({ type: TextDecorationType.Underline, color: Color.Black })
       Span(',我是Span3').fontSize(16).fontColor(Color.Grey)
         .decoration({ type: TextDecorationType.Overline, color: Color.Green })
}
.borderWidth(1)
.padding(10)

- Span通过textCase设置文字一直保持大写或者小写状态

Text() {
      Span('I am Upper-span').fontSize(12).textCase(TextCase.UpperCase)
}
.borderWidth(1)
.padding(10)

2.3.Span添加事件

Text() {
  Span('I am Upper-span').fontSize(12)
    .textCase(TextCase.UpperCase)
    .onClick(()=>{
      console.info('我是Span——onClick')
    })
}

3.1.TextInput的概述

TextInput 是单行输入框组件,通常用于响应用户的输入操作

3.2.TextInput的创建

TextInput()

- 设置输入框类型

TextInput有5种可选类型,分别为Normal基本输入模式、Password密码输入模式、Email邮箱地址输入模式、Number纯数字输入模式、PhoneNumber电话号码输入模式。通过type属性进行设置:

  • 基本输入模式(默认类型)
TextInput().type(InputType.Normal)
  • 密码输入模式
TextInput().type(InputType.Password)
  • Email邮箱地址输入模式
TextInput().type(InputType.Email)
  • Number纯数字输入模式
TextInput().type(InputType.Number)
  • PhoneNumber电话号码输入模式
TextInput().type(InputType.PhoneNumber)

- 自定义样式

  • 设置无输入时的提示文本
TextInput({placeholder:'我是提示文本'})
  • 设置输入框当前的文本内容
TextInput({placeholder:'我是提示文本',text:'我是当前文本内容'})
  • 添加backgroundColor改变输入框的背景颜色
TextInput({placeholder:'我是提示文本',text:'我是当前文本内容'}).backgroundColor(Color.Pink)

3.3.添加事件

TextInput()
  .onChange((value: string) => {
    console.info(value);
  })
  .onFocus(() => {
    console.info('获取焦点');
  })

4.1.TextArea的概述

TextArea 是 多行输入框

4.2.TextArea的创建

TextArea()

- 多行输入框文字超出一行时会自动折行

TextArea({text:"我是TextArea我是TextArea我是TextArea我是TextArea"}).width(300)

5.1.Button的概述

Button是按钮组件,通常用于响应用户的点击操作

5.2.Button的创建

Button通过调用接口来创建,接口调用有以下两种形式

  • 创建不包含子组件的按钮
Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 
  .borderRadius(8) 
  .backgroundColor(0x317aff) 
  .width(90)
  .height(40)
  • 创建包含子组件的按钮
Button({ type: ButtonType.Normal, stateEffect: true }) {
	  Row() {
	    Image($r('app.media.app_icon')).width(20).height(40).margin({ left: 12 })
	    Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
	  }.alignItems(VerticalAlign.Center)
}.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)

- 设置按钮类型

Button有三种可选类型,分别为Capsule(胶囊类型)、Circle(圆形按钮)和Normal(普通按钮),通过type进行设置

  • 胶囊按钮(默认类型)
Button('Disable', { type: ButtonType.Capsule, stateEffect: false })
      .backgroundColor(0x317aff)
      .width(90)
      .height(40)
  • 圆形按钮
Button('Circle', { type: ButtonType.Circle, stateEffect: false }) 
  .backgroundColor(0x317aff) 
  .width(90) 
  .height(90)
  • 普通按钮
Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 
  .borderRadius(8) 
  .backgroundColor(0x317aff) 
  .width(90)
  .height(40)

5.3.添加事件

Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 
  .onClick(()=>{ 
    console.info('Button onClick') 
  })

6.1.Image的概述

Image是显示图片组件,比如:按钮中的icon、网络图片、本地图片等

6.2.Image的获取图片的三种方式

  • 本地资源
Image('images/like.png').size({width: 100,height:100})
  • 网络资源
Image('https://www.wanandroid.com/resources/image/pc/logo.png').size({width: 300,height:300})

注:获取网络图片,需要申请网络权限,在 module.json5 中配置。

  • Resource资源
Image($r('app.media.like')).size({width: 100,height:100})

二、总结

  1. Text是文本组件,用于显示文字
  2. Span只能作为Text组件的子组件显示文本内容,可以在一个Text内添加多个Span来显示一段信息
  3. TextInput 是单行输入框组件,通常用于响应用户的输入操作
  4. TextArea 是 多行输入框
  5. Button是按钮组件,通常用于响应用户的点击操作
  6. Image是显示图片组件
  • 32
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谭祖爱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值