前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自学鸿蒙应用开发(6)- TextField组件

自学鸿蒙应用开发(6)- TextField组件

作者头像
面向对象思考
发布2020-12-31 15:01:59
6810
发布2020-12-31 15:01:59
举报

本文介绍在鸿蒙应用中TextField组件的基本用法。

增加TextField组件

如下代码中25行~32行所示,在布局中增加TextField组件。

代码语言:javascript
复制
代码语言:javascript
复制
代码语言:javascript
复制
<?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">
    <Component
        ohos:height="0vp"
        ohos:weight="3"
        ohos:width="match_parent"
        />
    <DirectionalLayout
        xmlns:ohos="http://schemas.huawei.com/res/ohos"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="center"
        ohos:orientation="vertical">
        <Image
            ohos:id="$+id:image"
            ohos:width="match_content"
            ohos:height="match_content"
            ohos:layout_alignment="center"
            ohos:image_src="$media:DevEco"
        />
        <TextField
            ohos:id="$+id:text_field"
            ohos:width="match_parent"
            ohos:height="30vp"
            ohos:text_size="20fp"
            ohos:hint="Please input text and press [Click me!] button."
            ohos:background_element="$graphic:background_text_field"
        />
        <Button
            ohos:id="$+id:hello_button"
            ohos:width="match_content"
            ohos:height="match_content"
            ohos:text_size="27fp"
            ohos:text="Click me!"
            ohos:layout_alignment="center"
            ohos:background_element="$graphic:background_button"
            ohos:margin="15vp"
            ohos:right_padding="8vp"
            ohos:left_padding="8vp"
            />
    </DirectionalLayout>
    <Component
        ohos:height="0vp"
        ohos:weight="5"
        ohos:width="match_parent"
        />
</DirectionalLayout>
代码语言:javascript
复制

代码中组件id被指定为text_field,会在下面的响应代码中用到。

获取TextField组件并取得输入文字信息

如下面代码中20行和22行所示,在按钮组件的响应代码中获取TextField组件后,获取输入的文字信息并弹窗显示。

代码语言:javascript
复制
代码语言:javascript
复制
package com.example.helloharmony.slice;
import com.example.helloharmony.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 ComponentAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_component);
        Button button = (Button) findComponentById(ResourceTable.Id_hello_button);
        // 为按钮设置点击事件回调
        button.setClickedListener(new Component.ClickedListener() {
            public void onClick(Component v) {
                TextField tf = (TextField) findComponentById(ResourceTable.Id_text_field);
                new ToastDialog(getContext())
                    .setText(tf.getText())
                    .show();
            }
        });
    }
    @Override
    public void onActive() {
        super.onActive();
    }
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
代码语言:javascript
复制
画面显示如下:

参考文档

TextField类:

https://developer.harmonyos.com/cn/docs/documentation/doc-references/textfield-0000001054358710

TextField组件:

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-textfield-0000001061125582

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档