当前位置: 首页 > news >正文

鸿蒙1.2:第一个应用

1、create Project,选择Empty Activity

在这里插入图片描述

2、配置项目

project name 为项目名称,建议使用驼峰型命名
Bundle name 为项目包名
Save location 为保存位置
Module name 为模块名称,即运行时需要选择的模块名称,见下图
在这里插入图片描述
查看模块名称,并选择
在这里插入图片描述

3、创建第一个界面

在这里插入图片描述
第一个界面实现:有一个Hello World的Text 和Button组成,点击button跳转到第二个界面中,button中带有字符串Next

import { router } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';@Entry
@Component
struct Index {@State message: string = 'Hello World';build() {Row(){Column() {Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)// 添加按钮,以响应用户点击Button() {Text('Next').fontSize(30).fontWeight(FontWeight.Bold)}.type(ButtonType.Capsule).margin({top: 20}).backgroundColor('#0D9FFB').width('40%').height('15%').onClick(() => {console.info(`Succeeded in clicking the 'Next' button.`)// 跳转到第二页router.pushUrl({ url: 'pages/second' }).then(() => {console.info('Succeeded in jumping to the second page.')}).catch((err: BusinessError) => {console.error(`Failed to jump to the second page. Code is ${err.code}, message is ${err.message}`)})})}.width('100%')}}
}

4、创建第二个界面

在resources -> base -> profile目录下的main_pages.jason中添加界面pages/second

{"src": ["pages/Index","pages/second"]
}

在这里插入图片描述

5、第二个界面的代码实现

使用 ‘string’ 表示的是字符串

import { router } from '@kit.ArkUI'
import { BusinessError } from '@kit.BasicServicesKit'// Second.ets
@Entry
@Component
struct Second {@State message: string = 'Hi there'build() {Row() {Column() {Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)Button() {Text('Back').fontSize(30).fontWeight(FontWeight.Bold)}.type(ButtonType.Capsule).margin({top: 20}).backgroundColor('#0D9FFB').width('40%').onClick(() => {console.info(`Succeeded in clicking the 'Back' button.`)// 跳转到第二页router.pushUrl({ url: 'pages/Index' }).then(() => {console.info('Succeeded in jumping to the first page.')}).catch((err: BusinessError) => {console.error(`Failed to jump to the first page. Code is ${err.code}, message is ${err.message}`)})}).height("15%")}.width('100%')}.height('100%')}
}

6、实现效果

第一个界面Index:

在这里插入图片描述

第二个界面second:

在这里插入图片描述

build运行

在Previewer运行时,如果代码出现问题,并不会运行失败,而是界面不重新刷新

解决方案:在Build -> Rebuild Project中重构代码,出现错误会提示

http://www.lryc.cn/news/514743.html

相关文章:

  • 2024年常用工具
  • 【蓝桥杯】走迷宫
  • 【pyqt】(三)designer
  • 【Go学习】-01-3-函数 结构体 接口 IO
  • 昆仑万维大数据面试题及参考答案
  • 20250103在Ubuntu20.04.5的Android Studio 2024.2.1.12中跑通Hello World
  • Hack The Box-Starting Point系列Three
  • 【Python其他生成随机字符串的方法】
  • redis7基础篇2 redis的主从模式1
  • Springboot - Web
  • 【C】​动态内存管理
  • lec5-传输层原理与技术
  • 【C语言】_指针运算
  • “AI智慧教学系统:开启个性化教育新时代
  • 商用车自动驾驶,迎来大规模量产「临界点」?
  • CSS 学习之正确看待 CSS 世界里的 margin 合并
  • 杰发科技——使用ATCLinkTool解除读保护
  • uni-app深度解码:跨平台APP开发的核心引擎与创新实践
  • unity团结云下载项目
  • Jmeter进阶篇(31)解决java.net.BindException: Address already in use: connect报错
  • 商米电子秤服务插件
  • 华为ensp-BGP路由过滤
  • Sigrity System SI SerialLink模式进行Pcie3协议仿真分析操作指导-pcie3_client_single_post
  • Python提取目标Json键值:包含子嵌套列表和字典
  • 分享6个对象数组去重的方法
  • Formality:官方Tutorial(一)
  • 力扣28找出字符串中第一个匹配项的下标
  • 【JAVA】java中将一个list进行拆解重新组装
  • 在 Windows 上使用 SSH 密钥访问 Linux 服务器
  • 小白0基础centos8安装docker