美文网首页零基础学鸿蒙编程
零基础学鸿蒙编程-通过WebView打开网页

零基础学鸿蒙编程-通过WebView打开网页

作者: 蓝不蓝编程 | 来源:发表于2021-11-03 19:02 被阅读0次

    什么是WebView

    WebView是用来打开网页的一种UI控件,可以在App内跳转到指定网址,而不是采用系统浏览器打开网页.

    基础样例

    效果图

    实现方案

    • activity
    public class MainAbilitySlice extends AbilitySlice {
        @Override
        public void onStart(Intent intent) {
            super.onStart(intent);
            super.setUIContent(ResourceTable.Layout_ability_main);
            WebView webView = (WebView) findComponentById(ResourceTable.Id_webView);
            webView.getWebConfig() .setJavaScriptPermit(true);  // 允许网页执行JavaScript
            final String url = "https://www.baidu.com";
            webView.load(url);
        }
    }
    
    • 布局文件ability_main.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <DirectionalLayout
        xmlns:ohos="http://schemas.huawei.com/res/ohos"
        ohos:height="match_parent"
        ohos:width="match_parent">
    
        <ohos.agp.components.webengine.WebView
            ohos:id="$+id:webView"
            ohos:height="match_parent"
            ohos:width="match_parent">
        </ohos.agp.components.webengine.WebView>
    </DirectionalLayout>
    
    • 增加权限
      修改config.json,添加如下权限:
    "reqPermissions": [
      {
        "name": "ohos.permission.INTERNET"
      }
    ]
    

    完整源代码

    https://gitee.com/hspbc/harmonyos_demos/tree/master/webviewDemo

    关于我

    厦门大学计算机专业 | 前华为工程师
    分享编程技术,没啥深度,但看得懂,适合初学者。
    Java | 安卓 | 前端 | 小程序 | 鸿蒙
    公众号:花生皮编程

    相关文章

      网友评论

        本文标题:零基础学鸿蒙编程-通过WebView打开网页

        本文链接:https://www.haomeiwen.com/subject/veqmaltx.html