网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
}
ts
2. 添加按钮。
在默认页面基础上,我们添加一个Button组件,作为按钮响应用户点击,从而实现跳转到另一个页面。“**Index.ets**”文件的示例如下:
// Index.ets
@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(‘5%’)
}
.width(‘100%’)
}
.height(‘100%’)
}
}
ts
3. 在编辑窗口右上角的侧边工具栏,点击Previewer,打开预览器。第一个页面效果如下图所示:
![zh-cn_image_0000001311334976](https://img-blog.csdnimg.cn/img_convert/c11a403741aee9ef557dbecaeb76acb5.webp?x-oss-process=image/format,png)
### 构建第二个页面
1. 创建第二个页面。
* 新建第二个页面文件。在“**Project**”窗口,打开“**entry > src > main > ets**”,右键点击“**pages**”文件夹,选择“**New > ArkTS File**”,命名为“**Second**”,点击“**Finish**”。可以看到文件目录结构如下:
![secondPage](https://img-blog.csdnimg.cn/img_convert/2169b1c74c578cb275bd36da9a49adad.webp?x-oss-process=image/format,png)
> **说明:**
>
> 开发者也可以在右键点击“**pages**”文件夹时,选择“**New > Page**”,则无需手动配置相关页面路由。
-
配置第二个页面的路由。在“Project”窗口,打开“entry > src > main > resources > base > profile”,在main_pages.json文件中的“src”下配置第二个页面的路由“pages/Second”。示例如下:
{ "src": [ "pages/Index", "pages/Second" ] } json
1. 添加文本及按钮。
参照第一个页面,在第二个页面添加Text组件、Button组件等,并设置其样式。“**Second.ets**”文件的示例如下:
// 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(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor(‘#0D9FFB’)
.width(‘40%’)
.height(‘5%’)
}
.width(‘100%’)
}
.height(‘100%’)
}
}
ts
### 实现页面间的跳转
页面间的导航可以通过[页面路由router]( )来实现。页面路由router根据页面url找到目标页面,从而实现跳转。使用页面路由请导入router模块。
1. 第一个页面跳转到第二个页面。
在第一个页面中,跳转按钮绑定onClick事件,点击按钮时跳转到第二页。“**Index.ets**”文件的示例如下:
// Index.ets
// 导入页面路由模块
import router from ‘@ohos.router’;
@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(‘5%’)
// 跳转按钮绑定onClick事件,点击时跳转到第二页
.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) => {
console.error(Failed to jump to the second page.Code is ${err.code}, message is ${err.message}
)
})
})
}
.width(‘100%’)
}
.height(‘100%’)
}
}
ts
2. 第二个页面返回到第一个页面。
在第二个页面中,返回按钮绑定onClick事件,点击按钮时返回到第一页。“**Second.ets**”文件的示例如下:
// Second.ets
// 导入页面路由模块
import router from ‘@ohos.router’;
@Entry
@Component
struct Second {
@State message: string = ‘Hi there’
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button() {
Text(‘Back’)
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor(‘#0D9FFB’)
.width(‘40%’)
.height(‘5%’)
// 返回按钮绑定onClick事件,点击按钮时返回到第一页
.onClick(() => {
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
02673967)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!