0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

HarmonyOS系统TextField组件基本用法

电子发烧友论坛 来源:电子发烧友论坛 作者:兮动人 2021-10-09 09:18 次阅读

1. TextField组件基本用法

组件说明:

是Text的子类,用来进行用户输入数据的

常见属性:

5e2291ba-2381-11ec-82a8-dac502259ad0.png

《TextField ohos:id=“$+id:text” ohos:height=“50vp” ohos:width=“319vp” ohos:background_element=“#FFFFFF” ohos:hint=“请输入信息” ohos:layout_alignment=“horizontal_center” ohos:text_alignment=“center” ohos:text_color=“#999999” ohos:text_size=“17fp” ohos:top_margin=“100vp”/》

2. TextField案例——获取文本输入框中的内容并进行Toast提示

通过TextField获取文本输入框中的内容并进行Toast提示

新建项目:TextFieldApplication

ability_main

《?xml version=“1.0” encoding=“utf-8”?》《DirectionalLayout xmlns:ohos=“http://schemas.huawei.com/res/ohos” ohos:height=“match_parent” ohos:width=“match_parent” ohos:background_element=“#F2F2F2” ohos:orientation=“vertical”》

《TextField ohos:id=“$+id:text” ohos:height=“50vp” ohos:width=“319vp” ohos:background_element=“#FFFFFF” ohos:hint=“请输入信息” ohos:layout_alignment=“horizontal_center” ohos:text_alignment=“center” ohos:text_color=“#999999” ohos:text_size=“17fp” ohos:top_margin=“100vp”/》

《Button ohos:id=“$+id:but” ohos:height=“47vp” ohos:width=“319vp” ohos:background_element=“#21a8FD” ohos:layout_alignment=“center” ohos:text=“获取信息” ohos:text_alignment=“center” ohos:text_color=“#FEFEFE” ohos:text_size=“24vp” ohos:top_margin=“77vp”/》

《/DirectionalLayout》

因为要在 onClick 方法中用到 TextField 和 Button 这两个组件,所以要把这两个组件移到成员位置,使其成为成员变量后,onClick 方法才能访问的到

MainAbilitySlice

package com.xdr630.textfieldapplication.slice;

import com.xdr630.textfieldapplication.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Button;import ohos.agp.components.Component;import ohos.agp.components.TextField;import ohos.agp.utils.LayoutAlignment;import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

TextField tf; Button but;

@Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main);

//1.找到文本组件框对象 tf = (TextField) findComponentById(ResourceTable.Id_text); //找到按钮组件对象 but = (Button) findComponentById(ResourceTable.Id_but);

//2.给按钮绑定点击事件 //当点击了按钮之后,就要获取文本输入框的内容 but.setClickedListener(this);

}

@Override public void onActive() { super.onActive(); }

@Override public void onForeground(Intent intent) { super.onForeground(intent); }

@Override public void onClick(Component component) { //当点击了按钮之后,获取文本输入框的内容 String message = tf.getText(); //利用一个Toast将信息弹出 ToastDialog td = new ToastDialog(this); //大小不用设置,默认是包裹内容的 //自动关闭不用设置,默认到了时间之后就自动关闭 //默认持续时间是 2秒

//设置Toast的背景 td.setTransparent(true); //位置(默认居中) td.setAlignment(LayoutAlignment.BOTTOM); //设置一个偏移 td.setOffset(0,200); //设置Toast内容 td.setText(message); //让Toast出现 td.show(); }}

运行:

3. TextField组件高级用法

3.1 密码的密文展示

当输入密码的时候会变成密文展示

ohos:text_input_type=“pattern_password”:表示输入的密码以密文的方式显示

基本使用:

《?xml version=“1.0” encoding=“utf-8”?》《DirectionalLayout xmlns:ohos=“http://schemas.huawei.com/res/ohos” ohos:height=“match_parent” ohos:width=“match_parent” ohos:orientation=“vertical” ohos:background_element=“#F2F2F2”》

《TextField ohos:height=“50vp” ohos:width=“319vp” ohos:hint=“请输入信息” ohos:text_size=“17fp” ohos:hint_color=“#999999” ohos:text_alignment=“center” ohos:top_margin=“100vp” ohos:layout_alignment=“horizontal_center” ohos:background_element=“#FFFFFF” ohos:text_input_type=“pattern_password”/》

《/DirectionalLayout》

3.2 基线的设置

有的时候文本输入框并不是一个框,而是下面有一条横线,这条线华为官方叫做 基线

把文本输入框使用横线表示,在上面加上一条基线,把输入框的背景颜色去掉

《TextField ohos:height=“50vp” ohos:width=“319vp” ohos:hint=“请输入信息” ohos:text_size=“17fp” ohos:hint_color=“#999999” ohos:text_alignment=“center” ohos:top_margin=“100vp” ohos:layout_alignment=“horizontal_center” ohos:text_input_type=“pattern_password” ohos:basement=“#000000” /》

如果以后看到一条基线,然后在输入一些数字信息,这还是 TextField 文本输入框组件,只不过是背景色没有设置,让它跟布局的颜色一致了,看不到背景而已

3.3 气泡的设置

当用鼠标长按选中输入的内容后,就会选中内容,前面的光标和后面的光标,以及中间选中的内容颜色会改变,华为官方给前、后的光标,以及没有选中内容状态下出现的小气球取名为气泡

《TextField ohos:height=“50vp” ohos:width=“319vp” ohos:hint=“请输入信息” ohos:text_size=“17fp” ohos:hint_color=“#999999” ohos:text_alignment=“center” ohos:top_margin=“100vp” ohos:layout_alignment=“horizontal_center” ohos:basement=“#000000” /》

可以设置左边、右边,以及没有选中情况下的气泡

气泡的图片、颜色都是可以自定义的

以下用到的图片可自取:

https://www.aliyundrive.com/s/wT22d1Vb1BV

把左、右,以及中间没有选中的气泡图片复制到 media 文件夹下

《TextField ohos:height=“50vp” ohos:width=“319vp” ohos:hint=“请输入信息” ohos:text_size=“17fp” ohos:hint_color=“#999999” ohos:text_alignment=“center” ohos:top_margin=“100vp” ohos:layout_alignment=“horizontal_center” ohos:basement=“#000000” ohos:element_selection_left_bubble=“$media:left” ohos:element_selection_right_bubble=“$media:right” ohos:element_cursor_bubble=“$media:bubble” ohos:selection_color=“#FF0000” /》

ohos:element_selection_left_bubble、ohos:element_selection_right_bubble分别设置左右气泡显示的图片

ohos:element_cursor_bubble:设置没有选中时的气泡图片

ohos:selection_color:设置选中时内容的颜色

运行:

4. TextField案例——长按查看密码明文

在一些APP中,登录界面密码输入框那里有个小眼睛,按住小眼睛后就可以看到密码的明文展示,松开小眼睛又恢复到密文状态了

把“小眼睛”改成Button组件,实现的逻辑原理也是一样的

需求分析:

按住按钮不松,将输入框中的密码变成明文

松开按钮之后,输入框中的密码变回密文

新建项目:TextFieldApplication3

ability_main

《?xml version=“1.0” encoding=“utf-8”?》《DirectionalLayout xmlns:ohos=“http://schemas.huawei.com/res/ohos” ohos:height=“match_parent” ohos:width=“match_parent” ohos:orientation=“vertical” ohos:background_element=“#F2F2F2” 》

《TextField ohos:id=“$+id:text” ohos:height=“50vp” ohos:width=“319vp” ohos:hint=“请输入密码” ohos:text_size=“17fp” ohos:hint_color=“#999999” ohos:text_alignment=“center” ohos:top_margin=“100vp” ohos:layout_alignment=“horizontal_center” ohos:background_element=“#FFFFFF” ohos:text_input_type=“pattern_password”/》 《Button ohos:id=“$+id:but” ohos:height=“47vp” ohos:width=“319vp” ohos:text=“查看密码” ohos:text_size=“24vp” ohos:text_color=“#FEFEFE” ohos:text_alignment=“center” ohos:background_element=“#21a8FD” ohos:top_margin=“77vp” ohos:layout_alignment=“center”/》

《/DirectionalLayout》

MainAbilitySlice

package com.xdr630.textfieldapplication3.slice;

import com.xdr630.textfieldapplication3.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Button;import ohos.agp.components.Component;import ohos.agp.components.InputAttribute;import ohos.agp.components.TextField;import ohos.multimodalinput.event.TouchEvent;

public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener {

TextField tf;

@Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main);

//1.找到两个组件对象 tf = (TextField) findComponentById(ResourceTable.Id_text); Button but = (Button) findComponentById(ResourceTable.Id_but);

//2.要给按钮绑定一个触摸事件 //因为在触摸事件中,才能获取到按下不松或松开 //单击事件——只能捕获到点击了一下 but.setTouchEventListener(this);

}

@Override public void onActive() { super.onActive(); }

@Override public void onForeground(Intent intent) { super.onForeground(intent); }

@Override //参数一:现在触摸的按钮 //参数二:动作对象 public boolean onTouchEvent(Component component, TouchEvent touchEvent) { int action = touchEvent.getAction();

if (action == TouchEvent.PRIMARY_POINT_DOWN){//表示按下不松的时候 //当按下不送的时候,将文本框中密码变成明文 tf.setTextInputType(InputAttribute.PATTERN_NULL); }else if (action == TouchEvent.PRIMARY_POINT_UP){//表示松开的时候 //当松开的时候,将文本框中的密码变回密文 tf.setTextInputType(InputAttribute.PATTERN_PASSWORD); } //true:表示触摸事件的后续动作还会进行触发 //false:表示触摸事件只触发第一个按下不松 return true; }}

运行:

5. TextField案例——搭建登录界面

新建项目:TextFieldApplication4

细节说明:

Text文本(忘记密码了?)组件默认是左边放置的,加上 ohos:layout_alignment=“right”就是右边放置了,同时也给个ohos:right_margin=“20vp”和右边的屏幕有些距离。如果ohos:layout_alignment=“right”属性不写,直接写ohos:right_margin=“20vp,那么ohos:layout_alignment=”right“属性就会失效,因为组件默认是放在左边的。

ability_main

《?xml version=”1.0“ encoding=”utf-8“?》《DirectionalLayout xmlns:ohos=”http://schemas.huawei.com/res/ohos“ ohos:height=”match_parent“ ohos:width=”match_parent“ ohos:orientation=”vertical“ ohos:background_element=”#F2F2F2“》

《TextField ohos:id=”$+id:username“ ohos:height=”50vp“ ohos:width=”319vp“ ohos:hint=”请输入用户名“ ohos:text_size=”17fp“ ohos:hint_color=”#999999“ ohos:text_alignment=”center“ ohos:top_margin=”100vp“ ohos:layout_alignment=”horizontal_center“ ohos:background_element=”#FFFFFF“/》

《TextField ohos:id=”$+id:password“ ohos:height=”50vp“ ohos:width=”319vp“ ohos:hint=”请输入密码“ ohos:text_size=”17fp“ ohos:hint_color=”#999999“ ohos:text_alignment=”center“ ohos:top_margin=”10vp“ ohos:layout_alignment=”horizontal_center“ ohos:background_element=”#FFFFFF“ ohos:text_input_type=”pattern_password“/》 《Text ohos:height=”match_content“ ohos:width=”match_content“ ohos:text=”忘记密码了?“ ohos:text_size=”17fp“ ohos:text_color=”#979797“ ohos:top_margin=”13vp“ ohos:layout_alignment=”right“ ohos:right_margin=”20vp“/》 《Button ohos:height=”47vp“ ohos:width=”319vp“ ohos:text=”登录“ ohos:text_size=”24fp“ ohos:text_color=”#FEFEFE“ ohos:text_alignment=”center“ ohos:background_element=”#21a8FD“ ohos:top_margin=”77vp“ ohos:layout_alignment=”horizontal_center“/》

《Button ohos:height=”47vp“ ohos:width=”319vp“ ohos:text=”注册“ ohos:text_size=”24fp“ ohos:text_color=”#FEFEFE“ ohos:text_alignment=”center“ ohos:background_element=”#21a8FD“ ohos:top_margin=”13vp“ ohos:layout_alignment=”horizontal_center“/》

《/DirectionalLayout》

责任编辑:haq

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 操作系统
    +关注

    关注

    37

    文章

    6266

    浏览量

    121842
  • 文本
    +关注

    关注

    0

    文章

    118

    浏览量

    16917
  • HarmonyOS
    +关注

    关注

    79

    文章

    1780

    浏览量

    29233

原文标题:【鸿蒙实战】HarmonyOS实战——TextField文本输入框组件基本使用

文章出处:【微信号:gh_9b9470648b3c,微信公众号:电子发烧友论坛】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    鸿蒙OS开发实例:【工具类封装-emitter组件间通信】

    `MyEmitterUtil` 是一个针对 HarmonyOS 的事件驱动编程封装类,主要用于组件间的通信和数据传递。
    的头像 发表于 03-27 22:13 99次阅读

    2024款鸿蒙OS 最新HarmonyOS Next_HarmonyOS4.0系列教程分享

    鸿蒙的出现,标志着中国科技的崛起。HarmonyOS就是我们说的华为鸿蒙系统,截止到2023年8月4日已有超过7亿台设备搭载了鸿蒙OS系统。据多家媒体报道,2024年国内有21所985大学都开设
    发表于 02-28 10:29

    HarmonyOS开发技术全面分析

    HarmonyOS 通过组件化和小型化等设计方法,支持多种终端设备按需弹性部署,能够适配不同类别的硬件资源和功能需求。支撑通过编译链关系去自动生成组件化的依赖关系,形成组件树依赖图,
    发表于 02-21 16:31

    了解鸿蒙OS Text组件

    文本(Text)是用来显示字符串的组件,在界面上显示为一块文本区域。Text 作为一个基本组件,有很多扩展,常见的有按钮组件 Button,文本编辑组件
    的头像 发表于 01-29 15:24 615次阅读
    了解鸿蒙OS Text<b class='flag-5'>组件</b>

    harmonyos和安卓的区别

    HarmonyOS是由华为开发的一款全场景分布式操作系统,而安卓(Android)则是由谷歌开发的移动操作系统。虽然两者都是操作系统,但它们在很多方面存在明显的区别。下面是关于
    的头像 发表于 01-10 17:55 1017次阅读

    pcb插针的用法

    pcb插针的用法
    的头像 发表于 12-13 16:58 656次阅读

    HarmonyOS鸿蒙原生应用开发设计- 服务组件

    HarmonyOS设计文档中,为大家提供了一些已经设计好的原生服务组件库,开发者可以根据需要直接引用。 开发者直接使用官方提供的服务组件库样式,既可以符合HarmonyOS原生应用的开
    发表于 10-24 16:12

    HarmonyOS/OpenHarmony原生应用-ArkTS万能卡片组件Radio

    单选框,提供相应的用户交互选择项。该组件从API Version 8开始支持。无子组件。 一、接口 Radio(options: {value: string, group: string}) 从
    发表于 10-13 17:21

    HarmonyOS/OpenHarmony原生应用-ArkTS万能卡片组件Stack

    堆叠容器,子组件按照顺序依次入栈,后一个子组件覆盖前一个子组件。该组件从API Version 7开始支持。可以包含子组件。 一、接口 St
    发表于 10-09 14:29

    HarmonyOS/OpenHarmony原生应用-ArkTS万能卡片组件Span

    作为Text组件的子组件,用于显示行内文本的组件。无子组件 一、接口 Span(value: string | Resource) 从API version 9开始,该接口支持在Ark
    发表于 10-07 17:50

    HarmonyOS/OpenHarmony原生应用-ArkTS万能卡片组件Badge

    可以附加在单个组件上用于信息标记的容器组件。该组件从API Version 7开始支持。 支持单个子组件。子组件类型:
    发表于 09-28 11:53

    基于Rust语言Hash特征的基础用法和进阶用法

    Rust语言是一种系统级编程语言,具有高性能、安全、并发等特点,是近年来备受关注的新兴编程语言。在Rust语言中,Hash是一种常用的数据结构,用于存储键值对。Rust语言提供了一系列的Hash特征
    的头像 发表于 09-19 16:02 724次阅读

    基于select!宏的进阶用法

    Tokio 是一个基于 Rust 语言的异步编程框架,它提供了一组工具和库,使得异步编程变得更加容易和高效。其中最重要的组件之一就是 select!宏。 select!宏是 Tokio 中的一个核心
    的头像 发表于 09-19 15:35 307次阅读

    Stream模块的基础用法和进阶用法

    有用。在本教程中,我们将介绍 Stream 模块的基础用法和进阶用法,并提供示例。 基础用法 在本节中,我们将介绍 Stream 模块的基础用法,并提供基础示例。 从 Vec 中创建
    的头像 发表于 09-19 15:33 697次阅读

    HarmonyOS NEXT新能力,一站式高效开发HarmonyOS应用

    的应用呈现在消费者面前。 为了帮助开发者更好地实现HarmonyOS应用的开发,华为面向开发者推出了由赋能套件,鸿蒙系统特征能力,设计资源套件、开发测试套件,运维套件以及开发者支持平台构成的全面解决
    发表于 08-14 15:08