HarmonyOS Page间跳转避坑

it2023-03-15  87

官方给的文档很模糊,看到要疯,什么都给一点,暂时还没有DEMO。 官网地址 w3cschool地址

这里截个w3cschool的图(内容一致的,w3cschool字小一点) 我截个图吐槽一下: 所以我到底怎么把值返回?文档里面没写。。。。

找了好久,终于在SDK目录下找到DOC,SDK目录这样找: File-Settings,找到图示位置,复制当前SDK地址。 打开SDK路径后,进入java/版本号/docs/default路径,找到index.html,打开下面位置的文件: 里面看到一堆包名,class文件点不了,嗯。。。。 在项目里面ctrl点一下类名 这个是包名,复制一下 ok,浏览器里ctrl+F查一查 找到啦,打开看看,继续ctrl+F找类名 这次我们可以查,presentForResult是怎么用了: 返回用这个,接着看一下setResult 停止用terminate(),OK,终于会了。

运行看看: 搞定。 附两个主要类的源码,两个xml放这里了

public class MainAbilitySlice extends AbilitySlice { private DirectionalLayout myLayout = new DirectionalLayout(this); private HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00101, "同页跳转"); @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_main_layout); // 加载XML布局 Button button = (Button) findComponentById(ResourceTable.Id_button); if (button != null) { // 为按钮设置点击回调 button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { HiLog.info(label,"同页跳转"); presentForResult(new SecondAbilitySlice(), new Intent(), 0); } }); } } @Override protected void onResult(int requestCode, Intent resultIntent) { super.onResult(requestCode, resultIntent); if (requestCode == 0){ HiLog.info(label,"返回了out"); if(resultIntent!=null){ String value = (String) resultIntent.getParams().getParam("test"); HiLog.info(label,"返回了"); HiLog.info(label,value); } } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } } public class SecondAbilitySlice extends AbilitySlice { private DirectionalLayout myLayout = new DirectionalLayout(this); private HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00101, "同页跳转"); @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_second_layout); // 加载XML布局 Button button = (Button) findComponentById(ResourceTable.Id_btn_back); if (button != null) { // 为按钮设置点击回调 button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { HiLog.info(label,"同页返回"); Intent data = new Intent(); data.setParam("test","value"); setResult(data); terminate(); } }); } } @Override protected void onInactive() { super.onInactive(); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }

main_layout.xml

<?xml version="1.0" encoding="utf-8"?> <DependentLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_parent" ohos:background_element="#000000"> <Text ohos:id="$+id:text" ohos:width="match_content" ohos:height="match_content" ohos:center_in_parent="true" ohos:text="I am black" ohos:text_color="white" ohos:text_size="32fp"/> <Button ohos:id="$+id:button" ohos:width="match_content" ohos:height="match_content" ohos:text_size="19fp" ohos:text="Next" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:right_padding="80vp" ohos:left_padding="80vp" ohos:text_color="white" ohos:background_element="$graphic:button_element" ohos:center_in_parent="true" ohos:align_parent_bottom="true"/> </DependentLayout> <?xml version="1.0" encoding="utf-8"?> <DependentLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_parent" ohos:background_element="#000000"> <Text ohos:id="$+id:txt_content" ohos:width="match_content" ohos:height="match_content" ohos:center_in_parent="true" ohos:text="I am page" ohos:text_color="white" ohos:text_size="32fp"/> <Button ohos:id="$+id:btn_back" ohos:width="match_content" ohos:height="match_content" ohos:text_size="19fp" ohos:text="Back" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:right_padding="80vp" ohos:left_padding="80vp" ohos:text_color="white" ohos:background_element="$graphic:button_element" ohos:center_in_parent="true" ohos:align_parent_bottom="true"/> </DependentLayout>

写在最后: 实际测试发现,同Page间跳转,如果按钮在onStart()初始化控件,terminate返回后,按钮失效,无法再次点击,读者可自行尝试在onForeground()重载页面控件。

补充:后面发现,官网地址 直接在这里的放大镜找类名有同样的效果,不用查原文档

按钮失败问题,看到ZZR在B站的视频下面有人评论说到:请求参数返回,拿到can not find focus,这个问题我也遇到了, ZZR回复:你是不是调整按钮的参数了,即使把按钮的大小调整成是内容大小了,现在有个bug就是调整了按钮的大小参数,会引发鸿蒙的一个bug,就是点击区域变得很小很小,在按钮的上边缘。你试试

实测还是不行,pageslice级别的还是有问题,ability级别的正常,如果有人解决,欢迎解惑。

最新回复(0)