新建鸿蒙项目
这里按照正常创建
创建第二个页面
因为新建一个项目之后就会自动创建一个hello word 的页面,所以我们只需要创建第二个页面,名字自己随便按照取名规范取
修改第一个页面,并添加跳转事件,跳转到第二个页面
打开MainAbilitySlice文件
按住ctrl点击这个就能打开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:alignment="center"
ohos:orientation="vertical">
<Text
ohos:width="match_content"
ohos:height="match_content"
ohos:text_size="100"
ohos:padding="20"
ohos:text="第一个页面"/>
<Button
ohos:id="$+id:but"
ohos:top_margin="50"
ohos:width="match_content"
ohos:height="match_content"
ohos:padding="20px"
ohos:layout_alignment="center"
ohos:text="这是一个按钮"
ohos:element_left="$media:icon"
ohos:text_color="#0000FF"
ohos:text_size="100"
ohos:background_element="$graphic:but_background"/>
</DirectionalLayout>
然后开始写按钮的点击事件
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
but1 = (Button) findComponentById(ResourceTable.Id_but);
but1.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent1 = new Intent();
Operation operation = new Intent.OperationBuilder()
.withBundleName("com.example.myapplication")
.withAbilityName(PageAbility.class)
.build();
intent1.setOperation(operation);
startAbility(intent1);
}
});
}
然后为了醒目一点,在第二个页面加一个Text
<?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:alignment="center"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text_helloworld"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main1"
ohos:layout_alignment="horizontal_center"
ohos:text="第二个页面"
ohos:text_size="40vp"/>
<Button
ohos:id="$+id:but2"
ohos:top_margin="50"
ohos:width="match_content"
ohos:height="match_content"
ohos:text_size="100"
ohos:element_left="$media:icon"
ohos:text="这是第二个按钮"
ohos:background_element="$graphic:but_background"/>
</DirectionalLayout>
然后运行,点击按钮,就可以跳转到第二个页面了
最后在从第二个页面点击跳转回第一个页面
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main1);
button = (Button) findComponentById(ResourceTable.Id_but2);
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent1 = new Intent();
Operation operation = new Intent.OperationBuilder()
.withBundleName("com.example.myapplication")
.withAbilityName(MainAbility.class)
.build();
intent1.setOperation(operation);
startAbility(intent1);
}
});
}
这里说明一下
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent1 = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")//这个是跳转设备的ID,如果不传,或者传递一个空的字符串,不是跳转设备
.withBundleName("com.example.myapplication")//这个是跳转
.withAbilityName(MainAbility.class)
.build();
intent1.setOperation(operation);
startAbility(intent1);
}
});