两个页面实现跳转
前期准备
布局
-
xml文件
标签表示展示不同的内容
文本 图片
按钮
-
Java代码
在这个目录下面新建一个文件
IDE会帮我们自动在这里添加
在config.json添加的内容
代码实现
第一页的内容
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
Button btn;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
// 1.找到按钮 id
btn = (Button) findComponentById(ResourceTable.Id_btn1);
// 2. 给按钮添加一个点击事件
/*
理解方式
1. 当使用btn这个按钮点击了的话,执行了本类函数的onClick
*/
btn.setClickedListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public void onClick(Component component) {
if (component == btn) {
Intent i = new Intent();
// 包含了要跳转的页面信息
Operation operation = new Intent.OperationBuilder()
.withDeviceId("") // 跳转到那个设备上,如果传递一个没有内容的字符串,表示跳转本机
.withBundleName("com.example.myapplication") // 我要跳转到那个页面,小括号里面写包名
.withAbilityName("com.example.myapplication.SecondAbility") // 要跳转的页面
.build(); // 上面的三个信息进行打包
// 把打包之后的operation设置到意图当中
i.setOperation(operation);
// 跳转页面
startAbility(i);
}
}
}
ability_main文件的内容
<?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_main"
ohos:layout_alignment="horizontal_center"
ohos:text="first_page"
ohos:text_size="40vp"
/>
<Button
ohos:id="$+id:btn1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="red"
ohos:text_size="40fp"
ohos:text="点我"
/>
</DirectionalLayout>
第二页的内容
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
public class SecondAbilitySlice extends AbilitySlice implements Component.ClickedListener {
Button btn;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
// 创建一个布局对象
DirectionalLayout dl = new DirectionalLayout(this);
// 创建文本对象
Text t = new Text(this);
// 设置内容
t.setText("这是第二个页面");
// 设置文字大小
t.setTextSize(55);
// 设置文字颜色
t.setTextColor(Color.BLUE);
// 把文本对象添加到布局栏中
dl.addComponent(t);
// 把布局添加到子见面中
// super.setUIContent(dl);
/*
1. 添加按钮
2. 查找按钮
3. 添加点击事件
*/
Button btn = new Button(this);
btn.setText("返回");
btn.setAutoFontSize(true);
btn.setTextColor(Color.RED);
btn.setId(12);
dl.addComponent(btn);
super.setUIContent(dl);
btnClick();
}
public void btnClick() {
btn = (Button) findComponentById(12);
btn.setClickedListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public void onClick(Component component) {
if (component == btn) {
Intent i = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("com.example.myapplication") // 我要跳转到那个页面,小括号里面写包名
.withAbilityName("com.example.myapplication.MainAbility")
.build();
i.setOperation(operation);
startAbility(i);
}
};
}