当前位置:   article > 正文

11.HarmonyOS鸿蒙app_page的显示跳转方法_鸿蒙跳转到activity

鸿蒙跳转到activity

11.HarmonyOS鸿蒙app_page的显示跳转方法,text文本触发点击事件

使用Intent和Operation对象

创建新项目后,再创建secondPageAbility

 

 ability_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:alignment="center"
  7. ohos:orientation="vertical">
  8. <Text
  9. ohos:id="$+id:text_main"
  10. ohos:height="match_parent"
  11. ohos:width="match_content"
  12. ohos:background_element="$graphic:background_ability_main"
  13. ohos:layout_alignment="horizontal_center"
  14. ohos:text="$string:main_title1"
  15. ohos:text_size="40vp"
  16. />
  17. </DirectionalLayout>

Secondability_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:alignment="center"
  7. ohos:orientation="vertical">
  8. <Text
  9. ohos:id="$+id:text_second"
  10. ohos:height="match_parent"
  11. ohos:width="match_content"
  12. ohos:background_element="#9090FF"
  13. ohos:layout_alignment="horizontal_center"
  14. ohos:text="$string:second_title"
  15. ohos:text_size="40vp"
  16. />
  17. </DirectionalLayout>

MainAbilitySlice.java

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.aafwk.content.Operation;
  6. import ohos.agp.components.Component;
  7. import ohos.agp.components.Text;
  8. public class MainAbilitySlice extends AbilitySlice {
  9. @Override
  10. public void onStart(Intent intent) {
  11. super.onStart(intent);
  12. super.setUIContent(ResourceTable.Layout_ability_main);
  13. Text text = (Text) findComponentById(ResourceTable.Id_text_main);
  14. text.setClickedListener(new Component.ClickedListener() {
  15. @Override
  16. public void onClick(Component component) {
  17. Intent _intent = new Intent();
  18. //创建Operation对象
  19. Operation operation = new Intent.OperationBuilder().withDeviceId("")
  20. .withBundleName("com.example.myapplication")
  21. .withAbilityName("com.example.myapplication.SecondPageAbility")
  22. .build();
  23. _intent.setOperation(operation);
  24. //启动ability
  25. startAbility(_intent);
  26. }
  27. });
  28. }
  29. @Override
  30. public void onActive() {
  31. super.onActive();
  32. }
  33. @Override
  34. public void onForeground(Intent intent) {
  35. super.onForeground(intent);
  36. }
  37. }

SecondPageAbilitySlice.java

//结束当前的Ability
terminateAbility();
  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.Component;
  6. import ohos.agp.components.Text;
  7. public class SecondPageAbilitySlice extends AbilitySlice {
  8. @Override
  9. public void onStart(Intent intent) {
  10. super.onStart(intent);
  11. super.setUIContent(ResourceTable.Layout_Secondability_main);
  12. Text text = (Text) findComponentById(ResourceTable.Id_text_second);
  13. text.setClickedListener(new Component.ClickedListener() {
  14. @Override
  15. public void onClick(Component component) {
  16. //结束当前的Ability
  17. terminateAbility();
  18. }
  19. });
  20. }
  21. @Override
  22. public void onActive() {
  23. super.onActive();
  24. }
  25. @Override
  26. public void onForeground(Intent intent) {
  27. super.onForeground(intent);
  28. }
  29. }

string的配置

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/216874
推荐阅读
相关标签
  

闽ICP备14008679号