鸿蒙 OS 开发零基础快速入门教程

news/2024/9/25 15:23:38/

视频课程:
在这里插入图片描述

东西比较多, 这里主要分享一些代码和案例.

开关灯效果案例: 开灯
在这里插入图片描述

开关灯效果案例: 关灯
在这里插入图片描述

Column 和 Row 的基本用法

@Entry
@Component
struct Index {@State message: string = '张三';build() {// 一行内容Row() {// 一列内容Column() {// 文本内容Text(this.message).fontSize(50)// 字体大小.fontWeight(FontWeight.Bold) // 字体粗细}.width("100%")}.height('100%')}
}

一行两列的布局

@Entry
@Component
struct Index {@State message: string = '张三';build() {// 一行内容Row() {// 一列内容Column() {// 文本内容Text(this.message).fontSize(50)// 字体大小.fontWeight(FontWeight.Bold) // 字体粗细}.width("50%")// 一列内容Column() {// 文本内容Text(this.message).fontSize(50)// 字体大小.fontWeight(FontWeight.Bold) // 字体粗细}.width("50%")}.height('100%')}
}

开关灯效果案例的基本实现

@Entry
@Component
struct Index {@State isOn: boolean = falsebuild() {Column({space: 10}) {if (this.isOn) {Image("pages/images/img_light.png").width(300).height(300)} else {Image("pages/images/img_dark.png").width(300).height(300)}Row({space: 30}) {Button("开灯").onClick(() => this.isOn = true)Button("关灯").onClick(() => this.isOn = false)}}.width("100%").height("100%").justifyContent(FlexAlign.Center)}
}

图标按钮案例

@Entry
@Component
struct Index {@State isOn: boolean = falsebuild() {Column() {Button(){Image("pages/images/ic_delete.png").width(25).height(25)}.width(50).height(50).type(ButtonType.Circle).backgroundColor(Color.Red).onClick(()=>console.log("删除成功"))}.width("100%").height("100%").justifyContent(FlexAlign.Center)}
}

自定义组件案例

@Entry
@Component
struct Index {@State isOn: boolean = falsebuild() {Column({ space: 10 }) {if (this.isOn) {Image("pages/images/img_light.png").width(300).height(300).borderRadius(20)} else {Image("pages/images/img_dark.png").width(300).height(300).borderRadius(20)}Row({ space: 30 }) {GreenButton().onClick(() => this.isOn = true)RedButton().onClick(() => this.isOn = false)}}.width("100%").height("100%").justifyContent(FlexAlign.Center)}
}@Component
struct RedButton {build() {Button({ type: ButtonType.Circle }) {Image("pages/images/icon_switch.png").width(25).height(25)}.width(50).height(50).backgroundColor(Color.Red)}
}@Component
struct GreenButton {build() {Button({ type: ButtonType.Circle }) {Image("pages/images/icon_switch.png").width(25).height(25)}.width(50).height(50).backgroundColor(Color.Green)}
}

自定义组件参数案例

@Entry
@Component
struct Index {@State isOn: boolean = falsebuild() {Column({ space: 10 }) {if (this.isOn) {Image("pages/images/img_light.png").width(300).height(300).borderRadius(20)} else {Image("pages/images/img_dark.png").width(300).height(300).borderRadius(20)}Row({ space: 30 }) {SwitchButton({ color: Color.Green }).onClick(() => this.isOn = true)SwitchButton().onClick(() => this.isOn = false)}}.width("100%").height("100%").justifyContent(FlexAlign.Center)}
}@Component
struct SwitchButton {color: Color = Color.Redbuild() {Button({ type: ButtonType.Circle }) {Image("pages/images/icon_switch.png").width(25).height(25)}.width(50).height(50).backgroundColor(this.color)}
}

组件文件案例

index.ets

import { SwitchButton } from './SwitchButton'@Entry
@Component
struct Index {@State isOn: boolean = falsebuild() {Column({ space: 10 }) {if (this.isOn) {Image("pages/images/img_light.png").width(300).height(300).borderRadius(20)} else {Image("pages/images/img_dark.png").width(300).height(300).borderRadius(20)}Row({ space: 30 }) {SwitchButton({ color: Color.Green }).onClick(() => this.isOn = true)SwitchButton().onClick(() => this.isOn = false)}}.width("100%").height("100%").justifyContent(FlexAlign.Center)}
}

SwitchButton.ets

@Component
export struct SwitchButton {color: Color = Color.Redbuild() {Button({ type: ButtonType.Circle }) {Image("pages/images/icon_switch.png").width(25).height(25)}.width(50).height(50).backgroundColor(this.color)}
}

http://www.ppmy.cn/news/1530339.html

相关文章

物联网迎来下半场,国产 IoTOS 打造企业级智能硬件云服务平台

如有需求,文末联系小编 氦氪云 IoTOS 是一套先进的企业级物联网解决方案平台,为万物互联提供可靠安全稳定的终端接入、协议适配、消息路由、数据存储和分析、应用使能等核心功能。面向物联网领域中的终端设备商、系统集成商、应用服务商、能力提供商等&a…

使用 React、Material-UI、Spring、MySQL、MyBatis 以及高德 API 模拟实时位置信息

要使用 React、Material-UI、Spring、MySQL、MyBatis 以及高德 API 模拟实时位置信息,你可以按以下步骤来实现: 目录 1. 前端 (React Material-UI) 2. 后端 (Spring Boot MyBatis MySQL) 3. 模拟实时位置数据 4. 前后端联调 1. 前端 (React Mat…

PHP API 框架:构建高效API的利器【电商API接口】

在当今快速发展的互联网时代,API(应用程序编程接口)已成为连接不同应用程序和服务的关键。PHP,作为一种流行的服务器端脚本语言,提供了多种强大的框架来简化API的开发。本文将介绍PHP API框架的重要性,以及…

mysqli_fetch_object() 和 mysqli_fetch_array() 函数的区别

mysqli_fetch_object() mysqli_fetch_object() 函数用于从结果集中取得一行作为对象返回。这个对象反映了数据库表中的一行,其中对象的属性对应着表的列名。这个函数对于面向对象编程(OOP)的 PHP 开发者来说非常方便,因为它允许你…

TikTok直播推流不精准该怎么办?跟IP有关系吗?

TikTok,这款风靡全球的短视频社交平台,其直播功能已成为众多创作者与品牌宣传的利器。然而,不少用户却遭遇了直播推流不精准的难题,这直接影响到直播的曝光和互动效果。那么,面对这一问题,我们该如何应对&a…

vue配置axios

axios 是一个基于 Promise 的 HTTP 客户端,适用于浏览器和 node.js 环境。它提供了易于使用的 API 来发送异步 HTTP 请求到 REST 端点并处理响应。axios 因其简洁的 API 和广泛的浏览器兼容性而广受欢迎。 步骤一: 下载axios 在打开vue项目的编辑器中打…

Vue ElemetUI table实现双击修改编辑某个内容

1、使用cell-dblclick事件&#xff0c;当双击时触发事件 <el-table cell-dblclick"handleCellDblClick" 2、单元格设置 主要重点为判断双击时切换input框&#xff0c;然后绑定ref&#xff0c;设置失去焦点时触发点方法&#xff0c;与按enter键触发点方法 <…

(14)关于docker如何通过防火墙做策略限制

关于docker如何通过防火墙做策略限制 1、iptables相关问题 在Iptables防火墙中包含四种常见的表&#xff0c;分别是filter、nat、mangle、raw。 filter&#xff1a;负责过滤数据包。 filter表可以管理INPUT、OUTPUT、FORWARD链。 nat&#xff1a;用于网络地址转换。 nat表…