鸿蒙OS应用开发之——页面间跳转,2024年最新2024年最新考点面试

54 阅读1分钟

xmlns:ohos="schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:background_element="#ffffff">

<Text

ohos:id="$+id:text"

ohos:height="match_content"

ohos:width="match_content"

ohos:center_in_parent="true"

ohos:text="Page One"

ohos:text_color="#000000"

ohos:text_size="32fp"/>

<Button

ohos:id="$+id:button"

ohos:height="match_content"

ohos:width="match_content"

ohos:align_parent_bottom="true"

ohos:background_element="$graphic:background_button"

ohos:bottom_margin="40vp"

ohos:bottom_padding="8vp"

ohos:center_in_parent="true"

ohos:left_padding="70vp"

ohos:right_padding="70vp"

ohos:text="Next"

ohos:text_color="#ffffff"

ohos:text_size="19fp"

ohos:top_padding="8vp"

/>

background_button

<shape

xmlns:ohos="schemas.huawei.com/res/ohos"

ohos:shape="rectangle">

<corners

ohos:radius="100"/>

<solid

ohos:color="#007DFF"/>

MainAbilitySlice.java

public class MainAbilitySlice extends AbilitySlice {

@Override

public void onStart(Intent intent) {

super.onStart(intent);

super.setUIContent(ResourceTable.Layout_ability_main);

Button button= (Button) findComponentById(ResourceTable.Id_button);

if (button!=null){

//为按钮设置点击回调

button.setClickedListener(new Component.ClickedListener() {

@Override

public void onClick(Component component) {

Intent secondIntent=new Intent();

// 指定待启动FA的bundleName和abilityName

Operation operation = new Intent.OperationBuilder()

.withDeviceId("")

.withBundleName("com.example.harmonyosdemo")

.withAbilityName("com.example.harmonyosdemo.SecondAbility")

.build();

secondIntent.setOperation(operation);

// 通过AbilitySlice的startAbility接口实现启动另一个页面

startAbility(secondIntent);

}

});

}

}

@Override

public void onActive() {

super.onActive();

}

@Override

public void onForeground(Intent intent) {

super.onForeground(intent);

}

}

MainAbility

public class MainAbility extends Ability {

@Override

public void onStart(Intent intent) {

super.onStart(intent);

super.setMainRoute(MainAbilitySlice.class.getName());

}

}

四 编写第2个页面


4.1 页面组成

  • entry > src > main > resources > base > layout——>ability_second.xml

  • entry > src > main > java > com.example.harmonyosdemo> slice>SecondAbilitySlice.java

  • entry > src > main > java > com.example.harmonyosdemo>SecondAbility.java

4.2 页面内容

ability_second.xml

<DependentLayout

xmlns:ohos="schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:background_element="#ffffff">

<Text

ohos:id="$+id:text"

ohos:height="match_content"

ohos:width="match_content"

ohos:center_in_parent="true"

ohos:text="Second Page "

ohos:text_color="#000000"

ohos:text_size="32fp"/>

<Button

ohos:id="$+id:buttonReturn"

ohos:height="match_content"

ohos:width="match_content"

ohos:align_parent_bottom="true"

ohos:background_element="$graphic:background_button"

ohos:bottom_margin="40vp"

ohos:bottom_padding="8vp"

ohos:center_in_parent="true"

ohos:left_padding="70vp"

ohos:right_padding="70vp"

ohos:text="Return"

ohos:text_color="#ffffff"

ohos:text_size="19fp"

ohos:top_padding="8vp"

/>

SecondAbilitySlice

public class SecondAbilitySlice extends AbilitySlice {

@Override

public void onStart(Intent intent) {

super.onStart(intent);

// 声明布局

super.setUIContent(ResourceTable.Layout_ability_second);

((Button)findComponentById(ResourceTable.Id_buttonReturn)).setClickedListener(new Component.ClickedListener() {

@Override

public void onClick(Component component) {

onBackPressed();

}

});

// DependentLayout myLayout = new DependentLayout(this);

// // 设置布局大小

// myLayout.setWidth(MATCH_PARENT);

// myLayout.setHeight(MATCH_PARENT);

// // 设置布局背景为白色

// ShapeElement element = new ShapeElement();

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

img img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取