『牛角书』 开发英汉词典APP02——界面组件变量定义

本文介绍了在鸿蒙系统下创建应用程序界面的过程,并演示了如何定义界面组件变量以及处理按钮点击事件,通过具体代码示例展示了TextField、Button和Text组件的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系列文章目录



前言

在创建完界面布局的组件后,我们需要对界面组件变量进行定义,并对按钮点击事件进行处理

一、定义界面组件变量

在MainAbilitySlice文件中对TextField,Button,Text组件进行定义
代码如下:
package edu.zut.myapplication.slice;

import edu.zut.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;

public class MainAbilitySlice extends AbilitySlice {

private TextField textField1;
private Button button1;
private Text resultText;

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

    textField1 = (TextField) findComponentById(ResourceTable.Id_TextField1);
    button1 = (Button) findComponentById(ResourceTable.Id_button1);
    resultText = (Text) findComponentById(ResourceTable.Id_resultText);
}

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

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

}

二、对按钮点击事件进行处理


 private static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP,0x00201,"MainAbilitySlice");

		button1.setClickedListener(new Component.ClickedListener() {
        @Override
        public void onClick(Component component) {
            String inputWord = textField1.getText();
            HiLog.info(LOG_LABEL,inputWord);
        }
    });

整体代码如下:
package edu.zut.myapplication.slice;

import edu.zut.myapplication.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.Text;
import ohos.agp.components.TextField;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {

private static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP,0x00201,"MainAbilitySlice");
private TextField textField1;
private Button button1;
private Text resultText;

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

    textField1 = (TextField) findComponentById(ResourceTable.Id_TextField1);
    button1 = (Button) findComponentById(ResourceTable.Id_button1);
    resultText = (Text) findComponentById(ResourceTable.Id_resultText);

    button1.setClickedListener(new Component.ClickedListener() {
        @Override
        public void onClick(Component component) {
            String inputWord = textField1.getText();
            HiLog.info(LOG_LABEL,inputWord);
        }
    });
}

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

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值