HarmonyOS开发30:TextField文本输入框组件基本用法

组件说明:

TextField文本输入框组件是Text的子类,用来进行用户输入数据的。

文本输入框是交互类组件

交互类组件:

  • 文本输入框TextField
    用户可以输入内容
  • 按钮Button
    用户可以点击
  • 多选框Checkbox
    用户可以选择
  • 单选框RadioButton
    用户可以选择
  • 滑块Slider
    用户可以滑动
  • 可滚动的视图ScrollView
    用户可以滚动阅读内容
  • 列表容器ListContainer
    以列表的形式展示数据
  • 搜索框SeachBar
    用来搜索的
  • 页面切换PageSlider
    多页面之间切换的组件

常见属性:

属性名称功能说明
hint提示文字
basement输入框基线的颜色
element_cursor_bubble设置提示气泡
selection_color选中文字的颜色
element_selection_left_bubble设置选中之后左边的气泡
element_selection_right_bubble设置选中之后右边的气泡
text_input_type输入框中的输入类型(pattern_password密文展示)

基本用法

ability_main.xml代码:

<?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:hint_color="#999999"
        ohos:layout_alignment="horizontal_center"
        ohos:text_alignment="center"
        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="24fp"
        ohos:top_margin="77vp"
        />

</DirectionalLayout>

MainAbilitySlice.java代码:

package com.example.textfieldapplication.slice;

import com.example.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();
        //利用吐司弹出信息
        ToastDialog td = new ToastDialog(this);
        //大小不用设置,默认包裹内容
        //自动关闭不用设置,默认到时间自动关闭
        //持续时间默认两秒钟
        //设置吐司背景:透明
        td.setTransparent(true);
        //位置(默认居中)
        td.setAlignment(LayoutAlignment.BOTTOM);
        //设置偏移
        td.setOffset(0,200);
        //设置内容
        td.setText(message);
        //吐司显示
        td.show();
    }
}

实现内容:点击获取信息按钮弹出吐司弹框,显示文本框中的内容

效果:
在这里插入图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,这是一个比较简单的界面实现问题。您可以使用Java Swing或JavaFX等GUI库来实现该界面。以下是一个JavaFX实现的示例代码: ```java import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.RadioButton; import javafx.scene.control.TextField; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.text.Text; import javafx.stage.Stage; public class MyGUI extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { // 创建文本输入框 TextField textField = new TextField(); textField.setPromptText("请输入7-10位文本"); // 创建RadioButton RadioButton radioButton1 = new RadioButton("选项1"); RadioButton radioButton2 = new RadioButton("选项2"); RadioButton radioButton3 = new RadioButton("选项3"); ToggleGroup toggleGroup = new ToggleGroup(); radioButton1.setToggleGroup(toggleGroup); radioButton2.setToggleGroup(toggleGroup); radioButton3.setToggleGroup(toggleGroup); // 创建CheckBox CheckBox checkBox1 = new CheckBox("选项1"); CheckBox checkBox2 = new CheckBox("选项2"); CheckBox checkBox3 = new CheckBox("选项3"); // 创建按钮 Button button = new Button("提交"); button.setOnAction(event -> { // 获取文本输入框内容 String text = textField.getText(); // 校验文本输入框内容 if (text.length() < 7 || text.length() > 10) { new Text("请输入7-10位文本").show(); return; } // 获取RadioButton的选中项 RadioButton selectedRadioButton = (RadioButton) toggleGroup.getSelectedToggle(); String radioButtonValue = selectedRadioButton == null ? "" : selectedRadioButton.getText(); // 获取CheckBox的选中项 String checkBoxValue = ""; if (checkBox1.isSelected()) { checkBoxValue += checkBox1.getText() + " "; } if (checkBox2.isSelected()) { checkBoxValue += checkBox2.getText() + " "; } if (checkBox3.isSelected()) { checkBoxValue += checkBox3.getText() + " "; } // 将组件内容显示到消息框中 System.out.println("文本输入框内容:" + text); System.out.println("RadioButton的选中项:" + radioButtonValue); System.out.println("CheckBox的选中项:" + checkBoxValue); }); // 创建布局 VBox vBox = new VBox(); vBox.setSpacing(10); vBox.setPadding(new Insets(10)); vBox.setAlignment(Pos.CENTER); vBox.getChildren().addAll(textField, radioButton1, radioButton2, radioButton3, checkBox1, checkBox2, checkBox3, button); // 创建场景 Scene scene = new Scene(vBox, 400, 400); // 设置舞台 primaryStage.setTitle("MyGUI"); primaryStage.setScene(scene); primaryStage.show(); } } ``` 这个界面包含了一个文本输入框、三个RadioButton、三个CheckBox和一个按钮组件文本输入框能够对输入文本进行位数校验提示(文本7-10位),点击按钮,能够把组件内容显示到消息框中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GeniusAng丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值