HarmonyOS系统开发ArkTS常用组件文本输入及参数

news/2024/6/3 18:01:56 标签: harmonyos, 华为

       TextInput文本输入组件,用于接收用户输入的文本内容。


1、TextInput组件的参数

       TextInput(value?:{placeholder?: string|Resource , text?: string|Resource})

  • placeholder属性用于设置无输入时的提示文本
  • text用于设置输入框当前的文本内容

@Entry
@Component
struct textInput {
  @State isOn: boolean = true;

  build() {
    Column({ space: 10 }) {
      Text('文本输入框 TextInput').fontSize(25)
      TextInput({'placeholder':'输入文本内容','text':''}).width(200).height(50)
    }
    .width('100%')
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

2、常用属性及事件

  • 输入框类型
  • 光标样式
  • 文本样式
  • change事件
  • 焦点事件

2.1 输入框类型

      可通过type()方法设置输入框的类型,该方法的参数为InputType枚举类型,可选的枚举值有


@Entry
@Component
struct textInput {
  @State isOn: boolean = true;

  build() {
    Column({ space: 10 }) {
      Text('文本输入框 TextInput').fontSize(25)
      TextInput({'placeholder':'基本输入','text':''}).type(InputType.Normal).width(200).height(50)
      TextInput({'placeholder':'数字','text':''}).type(InputType.Number).width(200).height(50)
      TextInput({'placeholder':'密码','text':''}).type(InputType.Password).width(200).height(50)
    }
    .width('100%')
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

2.2 placeholder样式

       placeholder样式还可通过placeholderFont()placeholderColor()方法设置 placeholder 的样式,其中placeholderFont()用于设置字体,包括字体大小、字体粗细等,placeholderColor()用于设置字体颜色


@Entry
@Component
struct textInput {
  @State isOn: boolean = true;

  build() {
    Column({ space: 10 }) {
      Text('文本输入框 TextInput').fontSize(25)
      TextInput({'placeholder':'基本输入','text':''})
        .placeholderFont({weight:'800'})
        .placeholderColor(Color.Orange)
        .type(InputType.Normal)
        .width(200)
        .height(50)

    }
    .width('100%')
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

2.3 光标样式

       可通过caretColor()方法设置光标的颜色

TextInput({'placeholder':'光标颜色','text':''}).type(InputType.Normal).caretColor(Color.Blue).width(200).height(50)

2.4 文本样式

       输入文本的样式可通过fontSize()fontWeight()fontColor()等通用属性方法进行设置

2.5 change事件

      每当输入的内容发生变化,就会触发 change 事件,开发者可使用onChange()方法为TextInput组件绑定 change 事件。

@Entry
@Component
struct textInput {
  @State isOn: boolean = true;

  build() {
    Column({ space: 10 }) {
      Text('文本输入框 TextInput').fontSize(25)
      TextInput({ 'placeholder': '光标颜色', 'text': '' })
        .type(InputType.Normal)
        .caretColor(Color.Blue)
        .onChange((content) => {
          console.log(content);
        })
        .width(200)
        .height(50)
    }
    .width('100%')
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}


2.6 焦点事件

      焦点事件包括获得焦点和失去焦点两个事件,当输入框获得焦点时,会触发 focus 事件,失去焦点时,会触发 blur 事件,开发者可使用onFocus()和onBlur()方法为 TextInput 组件绑定相关事件。

@Entry
@Component
struct textInput {
  @State isOn: boolean = true;

  build() {
    Column({ space: 10 }) {
      Text('文本输入框 TextInput').fontSize(25)
      TextInput({ 'placeholder': '光标颜色', 'text': '' })
        .type(InputType.Normal)
        .caretColor(Color.Blue)
        .onChange((content) => {
          console.log(content);
        })
        .onFocus(() => {
          console.log('输入框获得焦点');
        })
        .onBlur(() => {
          console.log('输入框失去焦点');
        })
        .width(200)
        .height(50)

      TextInput({ 'placeholder': '测试', 'text': '' })
        .width(200)
        .height(50)

    }
    .width('100%')
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}


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

相关文章

EFcore的实体类配置

1 约定配置 约定大于配置,框架默认了许多实体类配置的规则,在约定规则不满足要求时,可以显示地定义规则 1 数据库表明在不指定的情况下,默认使用的是数据库上下文类【DBContext】中DbSet 的属性名; 2 数据库表列的名字…

Java基础面试题(10)

136.什么是死锁(*)? 多个线程各自占用一些共享资源,并且互相等待其他线程占有的资源才能运行,而导致两个或多个线程都等待对方释放资源,都停止执行的情况,某一个同步块同时拥有“两个以 上对象的锁”时&am…

更安全的C gets()和str* 以及fgets和strcspn的用法

#include <stdio.h>int main() {char *str;gets(str);puts(str);return(0); }可以说全是错误 首先char *str没有指向一个分配好的地址&#xff0c;就直接读入&#xff0c;危险 ps: 怎么理解char *str "Hello World" 是将一个存储在一个只读的数据段中字符串常…

Zip压缩文件夹 + 前端导出

压缩本地文件成Zip&#xff0c;供前端导出 代码 /*** now 是当前时间戳* exportDir是导出目录路径 如D://export//111222333AA* param response* param now* param exportDir*/private void compressExportDirAndExport(HttpServletResponse response, long now, String expo…

IDEA中快速配置Git

Git介绍&#xff1a; Git下载 idea中配置Git

【在table里显示下拉框形式】

图&#xff1a; 一&#xff1a;主要是看审核方式&#xff0c;prop"status"和保存按钮的scope.row <el-table-column prop"status" label"审核方式" align"center"><template slot-scope"scope"><el-select v…

JVM学习——类的加载器以及类的加载过程

类加载器以及类的加载过程可分为三个阶段。 1、加载&#xff08;引导类&#xff0c;扩展类&#xff0c;系统类&#xff09; 2、链接&#xff08;验证&#xff0c;准备&#xff0c;解析&#xff09; 3、初始化 类加载系统只负责class的加载&#xff0c;不负责是否可以正常运行&…

WebSocket相关知识

WebSocket 是基于 TCP 的一种新的网络协议。它实现了浏览器与服务器全双工通信——浏览器和服务器只需要完成一次握手&#xff0c;两者之间就可以创建持久性的连接&#xff0c; 并进行双向数据传输。【应用场景&#xff1a;视频弹幕、网页聊天、体育实况更新、股票基金报价】 …