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

news2024/9/25 3:22:17

鸿蒙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.coloradmin.cn/o/1315251.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

linux 文本信息查询grep;控制命令执行和管道操作符号

1、grep grep "keyword" /path/to/logfile获取查询结果最后一行 grep "runs/detect/train" test4.log | tail -n 12、linux控制命令执行和管道操作符号 &、|、; 和 &&、》、>、< ##例子&#xff1b;wandb disabled && yolo …

synchronized关键字的使用和原理

synchronized关键字的使用和原理 synchronized&#xff1a;对象锁&#xff0c;保证了临界区内代码的原子性&#xff0c;采用互斥的方式让同一时刻至多只有一个线程能持有对象锁&#xff0c;其它线程获取这个对象锁时会阻塞&#xff0c;保证拥有锁的线程可以安全的执行临界区内…

vue-实现高德地图-省级行政区地块显示+悬浮显示+标签显示

<template><div><div id"container" /><div click"showFn">显示</div><div click"removeFn">移除</div></div> </template><script> import AMapLoader from amap/amap-jsapi-load…

SpringBoot - application.yml 多环境切换解决方案

问题描述 这个问题玩过 SpringCloud 的小伙伴估计会想到用 bootstrap.yml 来解决这个问题。但是如果说为了解决这个问题引入了一堆的 SpringCloud Jar&#xff0c;就感觉杀鸡用牛刀。 于是今天我们想只有 application.yml 自己就可以解决这个问题&#xff0c;如何搞定&#x…

真正可行的vue3迁移到nuxt3方法(本人亲测,完全避坑)

终于到了总结经验的时候了&#xff0c;这绝对是全网唯一、完全真正可行的干货。 在我看来&#xff0c;知识就是要拿来分享的&#xff0c;分享给他人也是在提高自己。我绝对不会搞什么订阅或者vip专栏来搞钱坑害各位&#xff0c; 因为我在csdn写文章最主要的目的是为了记录和总…

Chrome限制第三方Cookie:未来无法再追踪你看过哪些敏感的“色色”内容了

当我们在浏览网络的时候&#xff0c;常听到「Cookie」这个词&#xff0c;但许多人可能不太清楚它到底是什么。最近&#xff0c;Google 宣布了一项重要更新&#xff0c;Google Chrome 将减少对第三方cookie 的支持&#xff0c;以提高用户隐私保护。 下面我会解释一下这个改变对…

华为数通——路由冗余和备份

注&#xff1a;当一条路由的出接口down时&#xff0c;该路由会自动失效。 要求&#xff1a;数据优先走千兆链路。 R1 [ ]ip route-static 172.16.1.0 24 12.1.1.2 目的地址 掩码 下一条 [ ]ip route-static 172.16.1.0 24 21.1.1.2 preference 50 目的地址 …

ETLCloud的应用策略——实时数据处理是关键

一、ETLCloud是什么&#xff1f; ETLCloud又称数据集成&#xff08;DataOps&#xff09;&#xff0c;是RestCloud旗下的一款数据仓库管理工具&#xff0c;通过自动化数据转换和集成来实现企业内部和外部数据的无缝对接&#xff0c;从而帮助企业快速获取准确的数据信息&#xff…

vue3+leaflet天地图开发

<script setup> import { onMounted, onUnmounted, ref } from "vue"; // todo 项目使用请放开 leaflet 引入 // import L from leaflet;const emit defineEmits(["mapLoad"]);var markers ref([]); const mapRef ref(); const marker ref(); co…

Spring对JUnit4和junit5的支持

Junit4支持 第一步&#xff1a;准备工作&#xff1a; 引入JUnit4的依赖&#xff0c;Spring对JUnit支持的依赖还是&#xff1a;spring-test&#xff0c;如下&#xff1a; <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://ma…

Cadence SPB17.4 -Allegro - 做Logo封装及添加中文丝印

Cadence SPB17.4 -Allegro - 做Logo封装及添加中文丝印 Chapter1 Cadence SPB17.4 -Allegro - 做Logo封装Chapter2 Allegro添加中文字体的简单有效方法Chapter3 Allegro添加Logo方法方法一方法二 链接&#xff1a;https://pan.baidu.com/s/1eUgUOjOfNam3rqZyQOov_g 提取码&…

Java技术栈 —— Log4j 2、Logpack、SLF4j日志框架介绍

Log4j 2、Logpack、SLF4j日志框架介绍 Log4j 2、Logpack、SLF4j日志框架&#xff0c;及其区别1.1 Log4j 21.1.1 日志级别1.1.2 日志输出目标位置1.1.3 日志刷新机制1.1.4 结构化打印日志1.1.5 异步打印日志1.1.6 在Cloud云环境汇集日志信息 1.2 LogPack1.3 SLF4j1.4 区别 Log4j…

tcp连接全过程各种状态详解

文章目录 TCP的一些重要特性tcp连接全过程各种状态参考资料 TCP的一些重要特性 TCP是一种可靠、面向连接、全双工、流控制、拥塞控制、有序传输、无差错传输、无重复传输、无丢失传输等特点的协议。为了实现这些特点&#xff0c;TCP必须对上层应用程序发送的数据进行分段、重组…

玩转大数据18:大规模数据处理与分布式任务调度

引言 在数字化时代&#xff0c;数据成为了一种宝贵的资源&#xff0c;对于企业和组织来说&#xff0c;如何有效地处理和分析这些数据成为了关键的竞争力。大规模数据处理与分布式任务调度作为大数据处理的核心技术&#xff0c;为解决这一问题提供了有效的解决方案。 随着数据…

Python如何匹配库的版本

目录 1. 匹配库的版本 2. Python中pip&#xff0c;库&#xff0c;编译环境的问题回答总结 2.1 虚拟环境 2.2 pip&#xff0c;安装库&#xff0c;版本 1. 匹配库的版本 &#xff08;别的库的版本冲突同理&#xff09; 在搭建pyansys环境的时候&#xff0c;安装grpcio-tools…

阿赵UE学习笔记——1.安装UE

大家好&#xff0c;我是阿赵&#xff0c;这是一个新的开始&#xff0c;这次先简单介绍一下UE的安装方法。   安装Unreal Engine(简称UE)&#xff0c;需要先按照一个EPIC Games Launcher。 这个其实是EPIC的游戏中心&#xff0c;你可以理解成和Steam差不多&#xff0c;是一个…

盲盒小程序如何搭建?

随着移动互联网的发展&#xff0c;为了让消费者方便快捷地体验盲盒抽取乐趣&#xff0c;线上盲盒系统的开发成为了一个必要的过程。 今天本文将为大家介绍盲盒系统的搭建过程。 盲盒系统搭建过程 开发需求 在开发盲盒系统前&#xff0c;需要对盲盒市场深入分析&#xff0c;了…

IntelliJ IDEA 运行 若依分离版后端

一、本地运行 一、选择打开IntelliJ IDEA项目 二、选择若依项目 如&#xff1a;java123 三、等待右下角的准备工作&#xff08;有进度条的&#xff09;完成 四、修改MySQL 五、修改资源上传目录 六、修改redis 七、然后点击运行 八、成功图 九、测试访问 二、部署服务器运行 …

Linux-----12、时间日期

# 时间日期 # 时区设置 在Linux (opens new window)系统中&#xff0c;默认使用的是UTC时间。 即使在安装系统的时候&#xff0c;选择的时区是亚洲上海&#xff0c;Linux默认的BIOS时间&#xff08;也称&#xff1a;硬件时间&#xff09;也是UTC时间 (opens new window)。 在…

uniapp 单选按钮 选中默认设备

需求1&#xff1a;选中默认设备&#xff0c;113 和114 和139都可以选中一个默认设备 选中多个默认设备方法&#xff1a; async toSwitch(typeItem, title) {const res await this.setDefaultDev(typeItem.ibdr_devsn, typeItem.ibdr_pid)if (!res) {this.common.toast(切换默…