harmony开发之Text组件的使用

news/2024/7/21 12:01:10 标签: harmonyos

TextInput、TextArea是输入框组件,通常用于响应用户的输入操作,比如评论区的输入、聊天框的输入、表格的输入等,也可以结合其它组件构建功能页面,例如登录注册页面。

图片来源黑马程序员

Text组件的使用:

文本显示组件有两种方式,一种是字符串string,一种是读取指定的string格式的字符串!

可以实现,根据限定词,切换指定的国家语言,从而实现设备走向国家化!

Textinput组件的使用:

TextInput有5种可选类型,分别为Normal基本输入模式、Password密码输入模式、Email邮箱地址输入模式、Number纯数字输入模式、PhoneNumber电话号码输入模式。

设置无输入时的提示文本。

TextInput({placeholder:'我是提示文本'})

设置输入框当前的文本内容。

添加backgroundColor改变输入框的背景颜色。

源码部分如下:

@Entry
@Component
struct Index2 {
  @State imageWidth: number = 100

  build() {
    Column() {
      Row(){
        Image($r('app.media.icon'))
          .width(this.imageWidth)//控制图片的大小
      }
      .width('100')
      .height("100")
      .justifyContent(FlexAlign.Center)

      Row(){
        Text($r('app.string.width_label'))
          .fontSize(20)
          .fontWeight(FontWeight.Bold)

        TextInput({text: this.imageWidth.toFixed(0)})
          .width(150)
          .backgroundColor('#FFF')
          .type(InputType.Number)
          .onChange( value => {    //获取输入
            this.imageWidth = parseInt(value)
          })
      }
      .width('100%')
      .padding({left: 14, right: 14})
      .justifyContent(FlexAlign.SpaceBetween)

      Divider()
        .width('91%')

      Row(){
        Button('缩小')
          .width(80)
          .fontSize(20)
          .onClick(() => {
            if(this.imageWidth >= 10){
              this.imageWidth -= 10
            }
          })

        Button('放大')
          .width(80)
          .fontSize(20)
          .onClick(() => {
            if(this.imageWidth < 300){
              this.imageWidth += 10
            }
          })
      }
      .width('100%')
      .margin({ top: 35, bottom: 35 })
      .justifyContent(FlexAlign.SpaceEvenly)


      Slider({
        min: 100,
        max: 300,
        value: this.imageWidth,
        step: 10,
      })
        .width('100%')
        .blockColor('#36D')
        .trackThickness(5)
        .showTips(true)
        .onChange(value => {
          this.imageWidth = value
        })
    }
    .width('100%')
    .height('100%')
  }
}

文本框主要用于获取用户输入的信息,把信息处理成数据进行上传,绑定onChange事件可以获取输入框内改变的内容。

场景示例

用于表单的提交,在用户登录/注册页面,用户的登录或注册的输入操作。

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

TextArea(该组件从API Version 7开始支持。)

多行文本输入框组件,当输入的文本内容超过组件宽度时会自动换行显示。

除支持通用事件外(通用事件包含:宽高,内外边距。),还支持以下事件:

onCopy(callback:(value: string) => void)长按输入框内部区域弹出剪贴板后,点击剪切板复制按钮,触发该回调。当设置CopyOptions.None时,当前TextArea中的文字无法被复制或剪切,仅支持粘贴。

onCut(callback:(value: string) => void)长按输入框内部区域弹出剪贴板后,点击剪切板剪切按钮,触发该回调。

onPaste(callback:(value: string) => void)长按输入框内部区域弹出剪贴板后,点击剪切板粘贴按钮,触发该回调。

caretPosition(value: number): void    可以设置光标的位置。

示例代码如下:

// xxx.ets
@Entry
@Component
struct TextAreaExample {
  @State text: string = ''
  controller: TextAreaController = new TextAreaController()

  build() {
    Column() {
      TextArea({
        placeholder: 'The text area can hold an unlimited amount of text. input your word...',
        controller: this.controller
      })
        .placeholderFont({ size: 16, weight: 400 })//设置placeholder文本样式,包括字体大小,字体粗细,字体族,字体风格。目前仅支持默认字体族。
        .width(336)
        .height(56)
        .margin(20)
        .fontSize(16)
        .fontColor('#182431')
        .backgroundColor('#FFFFFF')
        .onChange((value: string) => {
          this.text = value
        })
      Text(this.text)
      Button('Set caretPosition 1')
        .backgroundColor('#007DFF')//背景颜色
        .margin(15)//边距
        .onClick(() => {
          // 设置光标位置到第一个字符后
          this.controller.caretPosition(1)
        })
    }.width('100%').height('100%').backgroundColor('#F1F3F5')
  }
}

以上信息,来自官网手册


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

相关文章

Android MVVM+coroutine+retrofit+flow+hilt

文章目录 Android MVVMcoroutineretrofitflowhilt概述依赖注入层数据层视图层模型视图层代码下载 Android MVVMcoroutineretrofitflowhilt 概述 代码结构&#xff1a; 依赖注入层 数据库&#xff1a; Module InstallIn(SingletonComponent::class) class DBModule {Singleto…

新手搭建知识付费平台必备攻略:如何以低成本实现高转化?

我有才知识付费平台 一、引言 随着知识经济的崛起&#xff0c;越来越多的知识提供者希望搭建自己的知识付费平台。然而&#xff0c;对于新手来说&#xff0c;如何以低成本、高效率地实现这一目标&#xff0c;同时满足自身需求并提高客户转化率&#xff0c;是一大挑战。本文将…

跨框架解决方案-Mitosis【Context】

Context Mitosis的context必须是&#xff1a; 在自己的文件中创建文件名必须以context.lite.ts结尾默认导出必须是一个返回context对象的函数 // simple.context.lite.ts import { createContext } from builder.io/mitosis;export default createContext({foo: bar,get foo…

ubuntu 修改系统时间,解决更新软件报错问题

ubuntu在更新软件时出现E: Release file for http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease 错误 网上解决方法一&#xff1a;修改系统时间 修改时区 timedatectl set-timezone Asia/Shanghai 查看当前时间 date -R date -s “2023-12-5 15:57:15” 查看…

【C++】:STL源码剖析之vector类容器的底层模拟实现

&#x1f4da;1.vector接口总览 namespace lyp {//模拟实现vectortemplate<class T>class vector{public:typedef T* iterator;typedef const T* const_iterator;//默认成员函数vector(); //构造函数vector(size_t n, const …

vue实现可拖拽列表

直接上代码 <!-- vue实现可拖拽列表 --> <template><div><button click"logcolig">打印数据</button><TransitionGroup name"list" tag"div" class"container"><divclass"item"v-f…

Excel函数(自用,工作中遇到过的)积累中

1.字符串拼接 CONCATENATE(text1,text2,...) tips&#xff1a;text使用“”来标识&#xff0c;如果文本中包含“&#xff0c;则需要转义\" eg: CONCATENATE("(",F2,",1,",B2,",",N2,",",M2,",",ROUND(M2/N2, 2),&qu…

一款Java实现的玩爆工具

这是一款涵盖娱乐到工作的软件程序&#xff0c;模块菜单包含&#xff1a;精选、博客园、观天下、听雨楼、短视频、电影、电视剧、藏金阁、云存储等诸多功能于一身的软件&#xff0c;下面我们来介绍一下软件的一些功能&#xff1a; 博客园&#xff1a;这是一个可以预览博客也可以…