鴻蒙應(yīng)用開發(fā)入門(六):頁(yè)面間跳轉(zhuǎn)
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz
1. 認(rèn)識(shí)Intent
Intent是對(duì)象之間傳遞信息的載體。
例如,當(dāng)一個(gè)Ability需要啟動(dòng)另一個(gè)Ability時(shí),或者一個(gè)AbilitySlice需要導(dǎo)航到另一個(gè)AbilitySlice時(shí),可以通過(guò)Intent指定啟動(dòng)的目標(biāo)同時(shí)攜帶相關(guān)數(shù)據(jù)。Intent的構(gòu)成元素包括Operation與Parameters:
2. 了解AbilitySlice路由配置
雖然一個(gè)Page可以包含多個(gè)AbilitySlice,但是Page進(jìn)入前臺(tái)時(shí)界面默認(rèn)只展示一個(gè)AbilitySlice。默認(rèn)展示的AbilitySlice是通過(guò)setMainRoute()方法來(lái)指定的。當(dāng)有多個(gè)需要展示的AbilitySlice,可以通過(guò)addActionRoute()方法為MainAbilitySlice以外的AbilitySlice配置路由規(guī)則。此時(shí),當(dāng)其他Page實(shí)例期望導(dǎo)航到這些AbilitySlice時(shí),可以通過(guò)AbilitySlice之間的跳轉(zhuǎn),顯示出這張頁(yè)面。
- public class MyAbility extends Ability {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- // set the main route
- setMainRoute(MainSlice.class.getName());
- // set the action route
- addActionRoute("action.pay", PaySlice.class.getName());
- addActionRoute("action.scan", ScanSlice.class.getName());
- }
- }
addActionRoute()方法中使用的動(dòng)作命名,需要在應(yīng)用配置文件(config.json)中注冊(cè):
- {
- "module": {
- "abilities": [
- {
- "skills":[
- {
- "actions":[
- "action.pay",
- "action.scan"
- ]
- }
- ]
- ...
- }
- ]
- ...
- }
- ...
- }
3. 同一個(gè)Page里的AbilitySlice1與AbilitySlice2間的跳轉(zhuǎn)(無(wú)參,帶參,回值)
1)無(wú)參數(shù)跳轉(zhuǎn)
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
- text.setClickedListener(component->{
- Intent intent1 = new Intent();
- present(new MainAbilitySlice1(),intent1);
- });
- }
2)帶參數(shù)跳轉(zhuǎn)
(1)產(chǎn)生參數(shù)端的AbilitySlice
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
- text.setClickedListener(component->{
- //有參數(shù)跳轉(zhuǎn)
- Intent intent1 = new Intent();
- intent1.setParam("user","鐘發(fā)發(fā)");
- present(new MainAbilitySlice1(),intent1);
- });
- }
(2)接收參數(shù)端的AbilitySlice
- public class MainAbilitySlice1 extends AbilitySlice {
- Text text;
- String oldText;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main1);
- text = (Text) findComponentById(ResourceTable.Id_text_helloworld1);
- if(intent != null){
- String user = intent.getStringParam("user");
- oldText = text.getText();
- text.append("," + user);
- }
- }
- @Override
- protected void onInactive() {
- super.onInactive();
- }
- ....
- }
3)帶參數(shù)跳轉(zhuǎn)+返回值
(1)參數(shù)產(chǎn)生端
- public class MainAbilitySlice extends AbilitySlice {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
- text.setClickedListener(component->{
- //有參數(shù)跳轉(zhuǎn)
- Intent intent1 = new Intent();
- intent1.setParam("user","鐘發(fā)發(fā)");
- presentForResult(new MainAbilitySlice1(),intent1,120);
- });
- }
- ...
- }
(2)參數(shù)接收端
- public class MainAbilitySlice1 extends AbilitySlice {
- Text text;
- String oldText;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main1);
- text = (Text) findComponentById(ResourceTable.Id_text_helloworld1);
- if(intent != null){
- String user = intent.getStringParam("user");
- oldText = text.getText();
- text.setText(oldText + "," + user);
- }
- //參數(shù)接收端在對(duì)文字點(diǎn)擊
- text.setClickedListener(component -> {
- //1.給跳轉(zhuǎn)來(lái)的頁(yè)面返回值
- Intent intent1 = new Intent();
- intent1.setParam("password","123456");
- setResult(intent1);
- //2.接收本AbilityAlice,自動(dòng)返回上一頁(yè)
- terminate();
- });
- }
- @Override
- protected void onInactive() {
- super.onInactive();
- text.setText(oldText);
- }
- ...
- }
(3)回到參數(shù)產(chǎn)生端接收返回值
- public class MainAbilitySlice extends AbilitySlice {
- Text text;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
- text.setClickedListener(component->{
- //有參數(shù)跳轉(zhuǎn)
- Intent intent1 = new Intent();
- intent1.setParam("user","鐘發(fā)發(fā)");
- presentForResult(new MainAbilitySlice1(),intent1,120);
- });
- }
- @Override
- protected void onResult(int requestCode, Intent resultIntent) {
- super.onResult(requestCode, resultIntent);
- if(requestCode==120){
- String password = resultIntent.getStringParam("password");
- text.setText("返回值:" + password);
- }
- }
- ...
- }
4. 不同的Page直接跳轉(zhuǎn),第一個(gè)鴻蒙應(yīng)用例子寫的就是這個(gè),核心代碼:
- if (button != null) {
- // 為按鈕設(shè)置點(diǎn)擊回調(diào)
- button.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- Intent secondIntent = new Intent();
- // 指定待啟動(dòng)FA的bundleName和abilityName
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("com.example.myapplication")
- .withAbilityName("com.example.myapplication.SecondAbility")
- .build();
- secondIntent.setOperation(operation);
- startAbility(secondIntent); // 通過(guò)AbilitySlice的startAbility接口實(shí)現(xiàn)啟動(dòng)另一個(gè)頁(yè)面
- }
- });
- }
5. Page1的MainAbilitySlice跳轉(zhuǎn)Page2的AbilitySlice1
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
- text.setClickedListener(component->{
- Intent intent1 = new Intent();
- intent1.setAction("abilityslice1"); //關(guān)鍵是配置文件里配置action和Ability里注冊(cè)路由
- startAbility(intent1);
- });
- }
文章內(nèi)容已錄制成視頻課程《鴻蒙手機(jī)應(yīng)用開發(fā)入門》https://edu.51cto.com/course/26133.html
©著作權(quán)歸作者和HarmonyOS技術(shù)社區(qū)共同所有,如需轉(zhuǎn)載,請(qǐng)注明出處,否則將追究法律責(zé)任
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz