零基础学鸿蒙编程

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

2021-11-03  本文已影响0人  蓝不蓝编程

什么是WebView

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

基础样例

效果图

实现方案

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);
    }
}
<?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>
"reqPermissions": [
  {
    "name": "ohos.permission.INTERNET"
  }
]

完整源代码

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

关于我

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

上一篇下一篇

猜你喜欢

热点阅读