成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

鴻蒙應用開發入門(六):頁面間跳轉

系統
文章由鴻蒙社區產出,想要了解更多內容請前往:51CTO和華為官方戰略合作共建的鴻蒙技術社區https://harmonyos.51cto.com/#zz

 [[373779]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com/#zz

1. 認識Intent

Intent是對象之間傳遞信息的載體。

例如,當一個Ability需要啟動另一個Ability時,或者一個AbilitySlice需要導航到另一個AbilitySlice時,可以通過Intent指定啟動的目標同時攜帶相關數據。Intent的構成元素包括Operation與Parameters:

2. 了解AbilitySlice路由配置

雖然一個Page可以包含多個AbilitySlice,但是Page進入前臺時界面默認只展示一個AbilitySlice。默認展示的AbilitySlice是通過setMainRoute()方法來指定的。當有多個需要展示的AbilitySlice,可以通過addActionRoute()方法為MainAbilitySlice以外的AbilitySlice配置路由規則。此時,當其他Page實例期望導航到這些AbilitySlice時,可以通過AbilitySlice之間的跳轉,顯示出這張頁面。

  1. public class MyAbility extends Ability { 
  2.  
  3. @Override 
  4.  
  5. public void onStart(Intent intent) { 
  6.  
  7. super.onStart(intent); 
  8.  
  9. // set the main route 
  10.  
  11. setMainRoute(MainSlice.class.getName()); 
  12.  
  13. // set the action route 
  14.  
  15. addActionRoute("action.pay", PaySlice.class.getName()); 
  16.  
  17. addActionRoute("action.scan", ScanSlice.class.getName()); 
  18.  
  19.  

 addActionRoute()方法中使用的動作命名,需要在應用配置文件(config.json)中注冊:

  1.  
  2. "module": { 
  3.  
  4. "abilities": [ 
  5.  
  6.  
  7. "skills":[ 
  8.  
  9.  
  10. "actions":[ 
  11.  
  12. "action.pay"
  13.  
  14. "action.scan" 
  15.  
  16.  
  17.  
  18.  
  19. ... 
  20.  
  21.  
  22.  
  23. ... 
  24.  
  25.  
  26. ... 
  27.  

 3. 同一個Page里的AbilitySlice1與AbilitySlice2間的跳轉(無參,帶參,回值)

1)無參數跳轉

  1. @Override 
  2.  
  3. public void onStart(Intent intent) { 
  4.  
  5. super.onStart(intent); 
  6.  
  7. super.setUIContent(ResourceTable.Layout_ability_main); 
  8.  
  9. Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld); 
  10.  
  11. text.setClickedListener(component->{ 
  12.  
  13. Intent intent1 = new Intent(); 
  14.  
  15. present(new MainAbilitySlice1(),intent1); 
  16.  
  17. }); 
  18.  

 2)帶參數跳轉

(1)產生參數端的AbilitySlice

  1. @Override 
  2.  
  3. public void onStart(Intent intent) { 
  4.  
  5. super.onStart(intent); 
  6.  
  7. super.setUIContent(ResourceTable.Layout_ability_main); 
  8.  
  9. Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld); 
  10.  
  11. text.setClickedListener(component->{ 
  12.  
  13. //有參數跳轉 
  14.  
  15. Intent intent1 = new Intent(); 
  16.  
  17. intent1.setParam("user","鐘發發"); 
  18.  
  19. present(new MainAbilitySlice1(),intent1); 
  20.  
  21. }); 
  22.  

 (2)接收參數端的AbilitySlice

  1. public class MainAbilitySlice1 extends AbilitySlice { 
  2.  
  3. Text text; 
  4.  
  5. String oldText; 
  6.  
  7. @Override 
  8.  
  9. public void onStart(Intent intent) { 
  10.  
  11. super.onStart(intent); 
  12.  
  13. super.setUIContent(ResourceTable.Layout_ability_main1); 
  14.  
  15. text = (Text) findComponentById(ResourceTable.Id_text_helloworld1); 
  16.  
  17. if(intent != null){ 
  18.  
  19. String user = intent.getStringParam("user"); 
  20.  
  21. oldText = text.getText(); 
  22.  
  23. text.append("," + user); 
  24.  
  25.  
  26.  
  27. @Override 
  28.  
  29. protected void onInactive() { 
  30.  
  31. super.onInactive(); 
  32.  
  33.  
  34. .... 
  35.  

 3)帶參數跳轉+返回值

(1)參數產生端

  1. public class MainAbilitySlice extends AbilitySlice { 
  2.  
  3. @Override 
  4.  
  5. public void onStart(Intent intent) { 
  6.  
  7. super.onStart(intent); 
  8.  
  9. super.setUIContent(ResourceTable.Layout_ability_main); 
  10.  
  11. Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld); 
  12.  
  13. text.setClickedListener(component->{ 
  14.  
  15. //有參數跳轉 
  16.  
  17. Intent intent1 = new Intent(); 
  18.  
  19. intent1.setParam("user","鐘發發"); 
  20.  
  21. presentForResult(new MainAbilitySlice1(),intent1,120); 
  22.  
  23. }); 
  24.  
  25.  
  26. ... 
  27.  

 (2)參數接收端

  1. public class MainAbilitySlice1 extends AbilitySlice { 
  2.  
  3. Text text; 
  4.  
  5. String oldText; 
  6.  
  7. @Override 
  8.  
  9. public void onStart(Intent intent) { 
  10.  
  11. super.onStart(intent); 
  12.  
  13. super.setUIContent(ResourceTable.Layout_ability_main1); 
  14.  
  15. text = (Text) findComponentById(ResourceTable.Id_text_helloworld1); 
  16.  
  17. if(intent != null){ 
  18.  
  19. String user = intent.getStringParam("user"); 
  20.  
  21. oldText = text.getText(); 
  22.  
  23. text.setText(oldText + "," + user); 
  24.  
  25.  
  26. //參數接收端在對文字點擊 
  27.  
  28. text.setClickedListener(component -> { 
  29.  
  30. //1.給跳轉來的頁面返回值 
  31.  
  32. Intent intent1 = new Intent(); 
  33.  
  34. intent1.setParam("password","123456"); 
  35.  
  36. setResult(intent1); 
  37.  
  38. //2.接收本AbilityAlice,自動返回上一頁 
  39.  
  40. terminate(); 
  41.  
  42. }); 
  43.  
  44.  
  45. @Override 
  46.  
  47. protected void onInactive() { 
  48.  
  49. super.onInactive(); 
  50.  
  51. text.setText(oldText); 
  52.  
  53.  
  54. ... 
  55.  

 (3)回到參數產生端接收返回值

  1. public class MainAbilitySlice extends AbilitySlice { 
  2.  
  3. Text text; 
  4.  
  5. @Override 
  6.  
  7. public void onStart(Intent intent) { 
  8.  
  9. super.onStart(intent); 
  10.  
  11. super.setUIContent(ResourceTable.Layout_ability_main); 
  12.  
  13. text = (Text)findComponentById(ResourceTable.Id_text_helloworld); 
  14.  
  15. text.setClickedListener(component->{ 
  16.  
  17. //有參數跳轉 
  18.  
  19. Intent intent1 = new Intent(); 
  20.  
  21. intent1.setParam("user","鐘發發"); 
  22.  
  23. presentForResult(new MainAbilitySlice1(),intent1,120); 
  24.  
  25. }); 
  26.  
  27.  
  28. @Override 
  29.  
  30. protected void onResult(int requestCode, Intent resultIntent) { 
  31.  
  32. super.onResult(requestCode, resultIntent); 
  33.  
  34. if(requestCode==120){ 
  35.  
  36. String password = resultIntent.getStringParam("password"); 
  37.  
  38. text.setText("返回值:" + password); 
  39.  
  40.  
  41.  
  42. ... 
  43.  

 4. 不同的Page直接跳轉,第一個鴻蒙應用例子寫的就是這個,核心代碼:

  1. if (button != null) { 
  2.  
  3. // 為按鈕設置點擊回調 
  4.  
  5. button.setClickedListener(new Component.ClickedListener() { 
  6.  
  7. @Override 
  8.  
  9. public void onClick(Component component) { 
  10.  
  11. Intent secondIntent = new Intent(); 
  12.  
  13. // 指定待啟動FA的bundleName和abilityName 
  14.  
  15. Operation operation = new Intent.OperationBuilder() 
  16.  
  17. .withDeviceId(""
  18.  
  19. .withBundleName("com.example.myapplication"
  20.  
  21. .withAbilityName("com.example.myapplication.SecondAbility"
  22.  
  23. .build(); 
  24.  
  25. secondIntent.setOperation(operation); 
  26.  
  27. startAbility(secondIntent); // 通過AbilitySlice的startAbility接口實現啟動另一個頁面 
  28.  
  29.  
  30. }); 
  31.  

 5. Page1的MainAbilitySlice跳轉Page2的AbilitySlice1

  1. @Override 
  2.  
  3. public void onStart(Intent intent) { 
  4.  
  5. super.onStart(intent); 
  6.  
  7. super.setUIContent(ResourceTable.Layout_ability_main); 
  8.  
  9. text = (Text)findComponentById(ResourceTable.Id_text_helloworld); 
  10.  
  11. text.setClickedListener(component->{ 
  12.  
  13. Intent intent1 = new Intent(); 
  14.  
  15. intent1.setAction("abilityslice1"); //關鍵是配置文件里配置action和Ability里注冊路由 
  16.  
  17. startAbility(intent1); 
  18.  
  19. }); 
  20.  

 文章內容已錄制成視頻課程《鴻蒙手機應用開發入門》https://edu.51cto.com/course/26133.html

©著作權歸作者和HarmonyOS技術社區共同所有,如需轉載,請注明出處,否則將追究法律責任

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com/#zz

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2021-09-18 14:45:26

鴻蒙HarmonyOS應用

2024-05-21 08:04:50

ArkTS鴻蒙應用開發語言

2010-05-11 16:55:12

Windows Pho

2023-07-31 17:35:31

ArkTS鴻蒙

2020-12-25 10:52:28

鴻蒙HarmonyOS應用開發

2021-01-25 09:58:01

鴻蒙HarmonyOS應用開發

2023-08-04 15:00:43

ArkTS語言鴻蒙

2020-12-24 12:01:16

鴻蒙HarmonyOS應用開發

2024-01-03 15:31:16

網格布局ArkTSGrid

2015-05-05 10:51:32

php頁面跳轉方法

2020-11-09 11:56:49

HarmonyOS

2020-09-28 15:13:04

鴻蒙

2021-06-22 16:40:32

鴻蒙HarmonyOS應用

2024-01-03 08:22:32

移動應用頁面跳轉

2021-01-06 11:21:56

鴻蒙HarmonyOS應用開發

2013-09-13 13:16:05

2022-03-15 08:00:00

Flutter開發工具

2022-11-04 14:58:59

應用開發鴻蒙

2021-05-18 09:49:08

鴻蒙HarmonyOS應用

2011-08-15 13:50:06

IPhone開發UIView動畫
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: av手机在线免费观看 | 在线观看电影av | 亚洲天堂精品一区 | 久久精品国产一区 | 欧美一a一片一级一片 | 卡通动漫第一页 | 日韩精品在线观看视频 | 一区二区日本 | 久久激情视频 | 99热在线免费 | 天天干夜夜操 | 久久久精| 国产精品久久久久久久久久妇女 | 拍真实国产伦偷精品 | 国产在线一| 日韩中文字幕一区二区 | 女同久久 | 欧美成人高清视频 | 成人在线小视频 | 精品二 | 日韩三区| 亚洲综合天堂网 | 午夜视频在线视频 | 日韩精品视频在线播放 | 久久中文一区二区 | 午夜在线视频一区二区三区 | 麻豆hd | 成人亚洲视频 | 久久精品一区二区 | www.日韩 | 亚洲精品自拍视频 | 久久亚洲一区 | 天天干狠狠干 | 亚洲一区二区在线 | 九九九视频在线观看 | 91资源在线播放 | 欧美8一10sex性hd | 不卡一区二区三区四区 | 欧美在线激情 | 91视频.| 久久国产成人 |