【鸿蒙 HarmonyOS】UI 组件 ( Button 组件 )

简介: 【鸿蒙 HarmonyOS】UI 组件 ( Button 组件 )

文章目录

一、布局文件中设置 Button 组件属性

二、代码中修改 Button 组件属性

三、Button 点击事件

四、完整代码示例

五、执行结果

六、GitHub 地址





一、布局文件中设置 Button 组件属性


Button 组件是在 UI 界面中的按钮组件 , 重要的用户交互接口 ;




布局文件中设置 Button :


Button 组件在布局文件中的示例 :


<?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">
    <Button
        ohos:id="$+id:button"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#000000"
        ohos:layout_alignment="horizontal_center"
        ohos:text="你点啥"
        ohos:text_size="150"
        ohos:text_color="#00FF00"/>
</DirectionalLayout>


id 属性 : ohos:id="$+id:button" , 用于作为当前组件的唯一标识 , 在单个布局文件中不允许 id 标识重复 ;


宽度与高度属性 : 可以设置 match_content 和 match_parent 两个值 ;


宽度 : ohos:width=“match_content”

高度 : ohos:height=“match_content”

组件位置属性 : ohos:layout_alignment=“horizontal_center” , 上述配置标识组件水平居中 ;


背景设置属性 : ohos:background_element="#000000" , 可以设置一个颜色值 ;


文本设置 : ohos:text=“你点啥” , 设置组件显示的文本为 “你点啥” ;


文本文字大小设置 : ohos:text_size=“150”


文本颜色设置 : ohos:text_color="#00FF00" , 绿色 ;






二、代码中修改 Button 组件属性


代码中设置 Button 属性 :


获取组件 : 调用 findComponentById ( ) 方法获取 ;


设置背景 : 需要使用 ShapeElement 对象设置 , 下面的代码是设置 Button 组件红色背景 ;


           

// 修改 Button 背景颜色
                ShapeElement shapeElement = new ShapeElement();
                // 设置红色背景
                shapeElement.setRgbColor(new RgbColor(0xFF, 0x00, 0x00));
                // 设置 组件 背景
                button.setBackground(shapeElement);


设置文本 : 调用 Button 对象的 setText ( ) 方法设置文本 ;


设置文字大小 : 调用 Button 对象的 setTextSize ( ) 方法设置文字大小 ;


设置文字颜色 : 调用 Button 对象的 setTextColor ( ) 方法设置文字颜色 ;


完整代码示例 : 设置 Button 组件红色背景 , 白色字体 , 180 大小文字 , 以及文本显示内容 ;


           

// 修改 Button 按钮属性
                // 修改 Button 背景颜色
                ShapeElement shapeElement = new ShapeElement();
                // 设置红色背景
                shapeElement.setRgbColor(new RgbColor(0xFF, 0x00, 0x00));
                // 设置 组件 背景
                button.setBackground(shapeElement);
                // 设置文本
                button.setText("点你咋地");
                // 设置文本颜色
                button.setTextColor(Color.WHITE);
                // 设置文本大小
                button.setTextSize(180);






三、Button 点击事件


点击 Button 按钮事件 :


设置 Component.ClickedListener 点击监听器 , 点击 Button 按钮组件后通过该方法回调 ;


 

// 获取 XML 布局中的 Button 按钮
        Button button = (Button) findComponentById(ResourceTable.Id_button);
        // 设置 Button 按钮点击事件
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
            }
        }






四、完整代码示例


主界面代码 :


package com.example.button.slice;
import com.example.button.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 获取 XML 布局中的 Button 按钮
        Button button = (Button) findComponentById(ResourceTable.Id_button);
        // 设置 Button 按钮点击事件
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
               // 修改 Button 按钮属性
                // 修改 Button 背景颜色
                ShapeElement shapeElement = new ShapeElement();
                // 设置红色背景
                shapeElement.setRgbColor(new RgbColor(0xFF, 0x00, 0x00));
                // 设置 组件 背景
                button.setBackground(shapeElement);
                // 设置文本
                button.setText("点你咋地");
                // 设置文本颜色
                button.setTextColor(Color.WHITE);
                // 设置文本大小
                button.setTextSize(180);
            }
        });
    }
    @Override
    public void onActive() {
        super.onActive();
    }
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}




布局文件代码 :


<?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">
    <Button
        ohos:id="$+id:button"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#000000"
        ohos:layout_alignment="horizontal_center"
        ohos:text="你点啥"
        ohos:text_size="150"
        ohos:text_color="#00FF00"/>
</DirectionalLayout>




五、执行结果


点击前 :

image.png



点击后 :


image.png






六、GitHub 地址


GitHub 主应用 : https://github.com/han1202012/HarmonyHelloWorld


Button 组件示例 Module : https://github.com/han1202012/HarmonyHelloWorld/tree/master/button


目录
相关文章
|
存储 缓存 5G
鸿蒙 HarmonyOS NEXT端云一体化开发-云存储篇
本文介绍用户登录后获取昵称、头像的方法,包括通过云端API和AppStorage两种方式,并实现上传头像至云存储及更新用户信息。同时解决图片缓存问题,添加上传进度提示,支持自动登录判断,提升用户体验。
631 1
|
存储 负载均衡 数据库
鸿蒙 HarmonyOS NEXT端云一体化开发-云函数篇
本文介绍基于华为AGC的端云一体化开发流程,涵盖项目创建、云函数开通、应用配置及DevEco集成。重点讲解云函数的编写、部署、调用与传参,并涉及环境变量设置、负载均衡、重试机制与熔断策略等高阶特性,助力开发者高效构建稳定云端服务。
1266 1
鸿蒙 HarmonyOS NEXT端云一体化开发-云函数篇
|
存储 JSON 数据建模
鸿蒙 HarmonyOS NEXT端云一体化开发-云数据库篇
云数据库采用存储区、对象类型、对象三级结构,支持灵活的数据建模与权限管理,可通过AGC平台或本地项目初始化,实现数据的增删改查及端侧高效调用。
834 1
|
存储 开发者 容器
鸿蒙 HarmonyOS NEXT星河版APP应用开发-ArkTS面向对象及组件化UI开发使用实例
本文介绍了ArkTS语言中的Class类、泛型、接口、模块化、自定义组件及状态管理等核心概念,并结合代码示例讲解了对象属性、构造方法、继承、静态成员、访问修饰符等内容,同时涵盖了路由管理、生命周期和Stage模型等应用开发关键知识点。
1017 1
鸿蒙 HarmonyOS NEXT星河版APP应用开发-ArkTS面向对象及组件化UI开发使用实例
鸿蒙 HarmonyOS NEXT星河版APP应用开发-阶段三
本文介绍了UI开发中的样式复用与组件构建技术,涵盖@Extend、@Styles和@Builder的使用方法,并通过Swiper轮播、Scroll滚动、Tabs导航等常用组件实现典型界面效果,结合生肖抽卡、小米轮播、回顶按钮等案例,展示实际应用技巧。
362 1
鸿蒙 HarmonyOS NEXT星河版APP应用开发-阶段二
本文介绍鸿蒙应用界面开发中的弹性布局(Flex)、绝对定位、层叠布局及ArkTS语法进阶,涵盖字符串拼接、类型转换、数组操作、条件与循环语句,并结合B站视频卡、支付宝首页等案例,深入讲解点击事件、状态管理与界面交互功能。
754 1
鸿蒙 HarmonyOS NEXT星河版APP应用开发-阶段二
|
开发者
鸿蒙应用开发从入门到实战(十二):ArkUI组件Button&Toggle
ArkUI提供了丰富的系统组件,用于制作鸿蒙原生应用APP的UI,本文主要讲解按钮组件Button和Toggle的使用。
833 2
|
设计模式 缓存 容器
06.HarmonyOS Next UI进阶:Text组件与视觉样式完全指南
在HarmonyOS Next应用开发中,Text组件是最基础也是最常用的UI元素之一。它不仅用于显示文本内容,还可以通过丰富的样式属性实现各种视觉效果。掌握Text组件的样式设置,是构建精美UI界面的基础技能。
703 1
【鸿蒙 HarmonyOS】UI 组件 ( 拖动条 Slider 组件 )
【鸿蒙 HarmonyOS】UI 组件 ( 拖动条 Slider 组件 )
753 0
【鸿蒙 HarmonyOS】UI 组件 ( 拖动条 Slider 组件 )
|
XML Java 数据格式
HarmonyOS学习路之开发篇—Java UI框架(自定义组件与布局 二)
自定义布局 当Java UI框架提供的布局无法满足需求时,可以创建自定义布局,根据需求自定义布局规则

热门文章

最新文章