鸿蒙app获取文本控件按钮控件_修改控件名称_按钮触发事件_提示信息显示

news/2024/7/21 11:40:51 标签: harmonyos, 华为

鸿蒙app获取文本控件按钮控件_修改控件名称_按钮触发事件_

点击启动:提示信息显示

package com.example.myapplication.slice;

import com.example.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.DirectionalLayout;

import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {
    static final HiLogLabel loglabel2 = new HiLogLabel(HiLog.LOG_APP,0x001101,"MainAbility");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        HiLog.info(loglabel2,"onStart-----------------------------------");
        int ppi = getContext().getResourceManager().getDeviceCapability().screenDensity;

        HiLog.info(loglabel2,"屏幕密度:"+Integer.toString(ppi));

      //  Text text2 = new Text();
        DirectionalLayout.LayoutConfig configForText = new DirectionalLayout.LayoutConfig(
                toPixels(100,getContext()),
                toPixels(16,getContext())
        );
        //text2.setLayoutConfig(configForText);//如果报错,则导入import ohos.agp.components.Text;
       // text2.set
        Text text1 = (Text) findComponentById(ResourceTable.Id_text_1);
        Text text2 = (Text) findComponentById(ResourceTable.Id_text_2);
        Text text3 = (Text) findComponentById(ResourceTable.Id_text_3);
        Text text3a = (Text) findComponentById(ResourceTable.Id_text_3a);
        text1.setText("美好时光1");
        text2.setText("万事如意");
        text3.setText("好运常在");
        text3a.setText("鸿蒙app demo");

        Button button = (Button) findComponentById(ResourceTable.Id_btn1);
        button.setText("启动");
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ToastDialog(getContext())
                        .setText("This is a ToastDialog")
                        .setAlignment(LayoutAlignment.CENTER)
                        .show();
            }
        });


    }
    /**
     *虚拟像素转物理像素
     */
    private  static int toPixels(int value, Context context)
    {
        return value * context.getResourceManager().getDeviceCapability().screenDensity/160;
    }
    @Override
    public void onActive() {
        super.onActive();
    }

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

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:scrollbar_background_color="#FF4385E7"
    ohos:orientation="vertical">
    <Button
        ohos:id="$+id:btn1"
        ohos:text="$string:btn_name"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size ="40vp"
        ohos:background_element="$graphic:capsule_button_element"
        ohos:layout_alignment="horizontal_center"

        />
    <Text
        ohos:id="$+id:text_1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="$string:mainability_HelloWorld"
        ohos:text_size="40vp"
        />
    <Text
        ohos:id="$+id:text_2"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="$string:mainability_HelloWorld2"
        ohos:text_size="40vp"
        />
    <Text
        ohos:id="$+id:text_3"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="$string:mainability_HelloWorld3"
        ohos:text_size="40vp"
        />
    <Text
        ohos:id="$+id:text_3a"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:text="$string:mainability_HelloWorld3a"
        ohos:text_size="40vp"
        ohos:layout_alignment="horizontal_center"
        />


</DirectionalLayout>


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

相关文章

SpringBoot报No qualifying bean of type ‘com.lzk.mapper.UserMapper‘ available

这个异常是SpringBoot程序没有找到要Bean&#xff0c;这里提示找不到UserMapper&#xff0c;有以下三种解决方式&#xff1a; 在UserMapper上添加注解MapperMapper public interface UserMapper extends BaseMapper<User> { }在启动类上进行Mapper包扫描&#xff0c;不用…

深入理解HTTP协议中的GET、POST、DELETE和PUT方法

在Web开发中&#xff0c;我们经常需要与服务器进行交互&#xff0c;以获取或发送数据。为了实现这一目标&#xff0c;我们使用HTTP协议。HTTP协议是一种无状态的、应用层的协议&#xff0c;它定义了客户端和服务器之间的通信方式。在HTTP协议中&#xff0c;有四种常用的请求方法…

Shell 常用命令详解-上

Shell 常用命令详解-上 1.目录查阅相关命令2.文件操作相关命令 1.目录查阅相关命令 ll 命令 命令描述&#xff1a;ll命令用于显示指定工作目录下的内容。 命令格式&#xff1a;ll [参数] [目录名]。 参数说明&#xff1a; 参数说明-a显示所有文件及目录&#xff08;包括隐藏文…

软件合集(项目开发中会用到的软件)

jeecg&#xff08;JeecgBoot 文档中心&#xff09; JeecgBoot是一款基于BPM的低代码平台&#xff01;前后端分离架构 SpringBoot 2.x&#xff0c;SpringCloud&#xff0c;Ant Design&Vue&#xff0c;Mybatis-plus&#xff0c;Shiro&#xff0c;JWT&#xff0c;支持微服务。…

openmmlab加载自训练权重

openmmlab加载自训练权重 在openmmlab中要加载自训练的模型权重&#xff0c;用于自己其他数据集训练的预训练模型。只需要在config文件中添加模型初始化。 在config.py文件中的model配置中初始化。 model dict(typeTopdownPoseEstimator,data_preprocessordict(),backboned…

100155. 双模幂运算 --力扣 --JAVA

题目 给你一个下标从 0 开始的二维数组 variables &#xff0c;其中 variables[i] [ai, bi, ci, mi]&#xff0c;以及一个整数 target 。 如果满足以下公式&#xff0c;则下标 i 是 好下标&#xff1a; 0 < i < variables.length((aibi % 10)ci) % mi target 返回一个由…

电镀污水处理设备有哪些

电镀污水处理设备是用来处理电镀过程中产生的废水&#xff0c;并将废水中的有害物质去除&#xff0c;使其达到排放标准的设备。通常&#xff0c;电镀污水处理设备包括以下几种类型&#xff1a; 1. 预处理设备&#xff1a;预处理设备通常包括过滤器、物理方法和化学方法等。过滤…

Linux主机自动注册NPS客户端(脚本化)

参考官方对API使用方法的定义:https://ehang-io.github.io/nps/#/ 1、首先必须要在配置文件中开启 auth_key 并配置一个合适的密钥 2、修改脚本中的可变量参数,以适配自己的环境 #!/bin/bash# 脚本名称:npc_create.sh # 编写人:清瞳清 # 编写时间:2023-12-10 # 描述:这…