翻開官網,找到WebView的文檔(模仿器不支撐)
鴻蒙webview的開闢指南(原始鏈接,便利大師辨認并點擊):https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-webview-0000001092715158
創立一個Page Ability,把基礎布局弄好
上面是代碼
?xml version= quot;1.0 quot; encoding= quot;utf-8 quot;?
DirectionalLayout xmlns:ohos= quot;http://schemas.huawei.com/res/ohos quot; ohos:height= quot;match_parent quot; ohos:width= quot;match_parent quot; ohos:orient包養金額ation= quot;vertical quot; DirectionalLayout ohos:height= quot;30vp quot; ohos:width= quot;match_parent quot; ohos:orientation= quot;horizontal quot; TextField ohos:id= quot;$+id:text_webView_Url quot; ohos:height= quot;match_conte包養appnt quot; ohos:width= quot;match_parent quot; ohos:background_element= quot;$graphic:background_ability_simple_web_view quot; ohos:focus_border_enable= quot;true quot; ohos:hint= quot;請輸出網址 quot包養合約; ohos:max_text_lines= quot;1 quot; oho包養條件s:multiple_lines= quot;false quot; ohos:scrollable= quot;true quot; ohos:text= quot;www.harmonyos.com quot; ohos:text_size= quot;50 quot; ohos:weight= quot;1 quot; / Button ohos:id= quot;$+id:button_webview_surf quot; ohos:height= quot;match_content quot; ohos:width= quot;“女兒說的是實話,其實因為婆婆對女兒真的很好,讓她有些不安。”藍玉華一臉疑惑的對媽媽說道。60vp quot; ohos:background_element= quot;$graphic:button_element quot; ohos:text= quot;跳轉 quot; ohos:text_size= quot;50 quot;/ /DirectionalLayout ProgressBar ohos:id= quot;$+id:other_webView台灣包養_progressBar quot; ohos:height= quot;10vp quot; ohos:width= quot;match_parent quot; ohos:visibility= quot;hide quot; /ProgressBar ohos.agp.components.webengine.WebView ohos:id= quot;$+id:webview_webview_webview quot; ohos:height= quot;match_parent quot; ohos:width= quot;match_parent quot; ohos:weight= quot;1 quot; /ohos.agp.components.webengine.WebView DirectionalLayout ohos:height= quot;30vp quot; ohos:width= quot;match_parent quot; ohos:orientation= quot;horizontal quot; DirectionalLayout ohos:height= quot;match_content quot; ohos:width= quot;match_parent quot; ohos:orientation= quot;horizontal quot; ohos:weight= quot;1 quot; Button ohos:id= quot;$+id:button_webview_back quot; ohos:height= quot;match_content quot; ohos:width= quot;match_parent quot; ohos:background_element= quot;$graphic:button_element quot; 包養網VIP ohos:layout_alignment= quot;horizontal_center quot; ohos:text= quot;向后 quot; ohos:text_size= quot;50 quot; /Button /DirectionalLayout DirectionalLayout ohos:height= quot;match_content quot; ohos:width= quot;match_parent quot; ohos:orientation= quot;horizontal quot; ohos:weight= quot;1 quot; Button ohos:id= quot;$+id:button_webview_refresh quot; ohos:height= quot;match_content quot; ohos:width= quot;match_parent quot; ohos:background_element= quot;$graphic:button_element quot; ohos:layout_alignment= quot;horizontal_center quot; ohos:text= quot;刷新 quot; ohos:text_size= quot;50 quot; /Button /DirectionalLayout DirectionalLayout ohos:height= quot;match_content quot; ohos:width= quot;match_parent quot; ohos:orientation= quot;horizontal quot; ohos:weight= quot;1 quot; Button ohos:id= quot;$+id:button_webview_forward quot; ohos:height= quot;match_content quot; ohos:width= quot;match_parent quot; ohos:background_element= quot;$graphic:button_element quot; ohos:layout_alignment= quot;horizontal_center quot; ohos:text= quot;向前 quot; ohos:text_size= quot;50 quot; /Bu包養apptton /DirectionalLayout /DirectionalLayout /DirectionalLayout
把基礎的按鈕事務弄好
序號按鈕效能
1跳轉把文本框中的網址翻開
2后退在webview中點了新鏈接后,想歸去看一看
3刷新以前的人在收集欠好,美男圖片出不來的時辰用,此刻普通是發了一篇帖子后,作者會沒事點一下,了解一下狀況有沒有人點贊
4進步跟后退是聯繫關係用的,就是點了新鏈接,歸去看了看后,仍是感到新鏈接更都雅,就又要進步.代碼
Component.ClickedListener clickedListener = new Component.ClickedListener() { @Override public void onClick(Component component) { int componentId = component.getId(); switch (componentId) { case ResourceTable.Id_button_webview_surf: { urlAddress = textWebViewUrl.getText(); if (urlAddress.isEmpty()) { return; } if (!urlAddress.startsWith(FinalValue.URL_HTTPS)) { urlAddress = FinalValue.URL_HTTPS + urlAddress; } webView.load(urlAddress); } break; case ResourceTable.Id_button_webview_back: { if (webView.getNavigator().canGoBack()) { webView.getNavigator().goBack(); } } break; case ResourceTable.Id_button_webview_refresh: { webView.reload(); } break; case ResourceTable.Id_button_webview_forward: { if (webView.getNavigator().canGoForward()) { webView.getNavigator().goForward(); } } break; default: { System.out.println( quot;沒有選擇任何的頁面 quot;); } break; } } };
把WebView照文檔下面的請求弄好
沒啥好說的,就是規則.我加在了挪用load方式翻開網址那行代碼后面,我還弄了一個跟進度條聯繫關係的效能
//答應javascript交互
WebConfig webConfig = webView.g女大生包養俱樂部etWebConfig(); webConfig.setDataAbilityPermit(true); webConfig.setJavaScriptPermit(true); webConfig.setLoadsImagesPermit(true); webConfig.setMediaAutoReplay(true); webConfig.setLocationPermit(true); webConfig.setSecurityMode(WebConfig.SECURITY_SELF_ADAPTIVE); webView.setWebAgent(new WebAgent() { @Override public void onLoadingPage(WebView webView, String url, PixelMap favicon) { super.onLoadingPage(webView, url, favicon); // 這兒我加了一個更換新的資料網址文本框中新頁面url的效能 if (url != urlAddress) { textWebViewUrl.setText(url); } } @Override public void onPageLoaded(WebView webView, String url) { super.onPageLoaded(webView, url); // 頁面加載停止后自界說處置 } @Override public void onLoadingContent(WebView webView, String url) { super.onLoadingContent(webView, url); // 加載資本時自界說處置 } @Override public void onError(WebView webView, ResourceRequest request, ResourceError error) { super.onError(webView, request, error); // 產生過錯時自界說處置 包養平台 } }); webView.setBrowserAgent(new BrowserAgent(SimpleWebVi包養網評價ewAbilitySlice.this) { @Override public void onTitleUpdated(WebView webView, String title) { super.onTitleUpdated(webView, title); // 題目變革時自界說處置 } @Override public void onProgressUpdated(WebView webView, int newProgress) { super.onProgressUpdated(webView, newProgress); if (newProgress FinalValue.PROGRESS_BAR_FINISHED) { otherWebViewProgressBar.setVisibility(Component.VISIBLE); otherWebViewProgressBar.setProgressValue(newProgress); } else if (newProgress == FinalValue.PROGRESS_BAR_FINISHED) { otherWebViewProgressBar.setVisibility(Component.HIDE); } // 加載進度變革時自界說處置 } });
完事?or完了還有事?
從下面拷代碼的話,估量完事了.可是,我是用的回想,可是代碼卻沒有回退,所以我仍是有需要在這兒把步調中的題目說一說,便利不拷代碼的同窗也能跑出一個界面.重要表現如下:
權限設置裝備擺設,台灣包養網未幾說
quot;reqPermissions quot;: [
{ quot;name quot;: quot;ohos.permission.INTERNET quot; } ]
xml中的WebView要帶包名
ohos.agp.components.webengine.WebView
ohos:id= quot;$+id:webview_webview_webview quot; ohos:height= quot;match_parent quot; ohos:width= quot;match_parent quot; ohos:weight= quot;1 quot; /ohos.agp.components.webengine.WebView
不按下面包名寫的話:
真機運轉后沒有WebView的界面.哪怕weight=1,也不可點擊跳轉按鈕后sd包養,PageAbility會閃退,回到首屏(挪用它的頁面)
完事後果:https://www.bilibili.com/video/BV1tK4y1o7Hz/
完全代碼
布局
序號為2的步調中貼全了
按鈕佈景
?xml version= quot;1.0 qu包養意思ot; encoding= quot;UTF-8 quot; ?
shape xmlns:ohos= quot;http://schemas.huawei.com/res/ohos quot; ohos:shape= quot;rectangle quot; corners ohos:radius= quot;20 quot;/ solid ohos:color= quot;#70dbdb quot;/ /shape
java代碼
package com.javaaier.family.huawei.slice;
import com.javaaier.family.huawei.ResourceTable;import com.javaaier.family.huawei.common.FinalValue;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.*;import ohos.agp.components.為,根本不會發生那種事情,事後,女兒連反省和懺悔都不知道,把所有的責任都推到下一個人身上,彩煥一直都是盡心盡力webengine.*;import ohos.media.image.PixelMap;/** * @author JavaAIer * @Description : webview控件例子1:用于簡略的測試webview的用法 br/ * 001 簡略webview示例 * @包養sdDate: 2021/4/16 */public class SimpleWebViewAbilitySlice extends AbilitySlice { String urlAddress; ProgressBar otherWebViewProgressBar; TextField textWebViewUrl; Button buttonWebViewS包養妹urf, buttonWebViewBack, bu包養情婦ttonWebViewRefresh, buttonWebViewForward; WebView webView; Component.ClickedListener clickedListener = new Component.ClickedListener() { @Override public void onClick(Component component) { int componentId = component.getId(); switch (componentId) { case ResourceTable.Id_button_webview_surf: { urlAddress = textWebViewUrl.getText(); if (urlAddress.isEmpty()) { return; } if (!urlAddress.startsWith(FinalValue.URL_HTTPS)) { urlAddress = FinalValue.URL_HTTPS + urlAddress; } webView.load(urlAddress); //答應javascript交互 WebConfig webCo包養心得nfig = webView.getWebConfig(); webConfig.setDataAbili長期包養tyPermit(true); webConfig.setJavaScriptPermit(true); webConfig.setLoadsImagesPermit(true); webConfig.setMediaAutoReplay(true); webConfig.setLocationPermit(true); webConfig.setSecurityMode(WebConfig.SECURITY_SELF_ADAPTIVE); webView.setWebAgent(new WebAgent() { @Override public void onLoadingPage(WebView webView, String url, PixelMap favicon) { super.onLoadingPage(webView, url, favicon); // 頁面開端加載時自界說處置 if (url != urlAddress) { textWebViewUrl.setText(url); } } @Override public void onPageLoaded(WebView webView, String url) { super.onPageLoaded(webView, url); // 頁面加載停止后自界說處置 } @Override public void onLoadingContent(WebView webView, String url) { 包養網單次 super.onLoadingContent(webView, url); // 加載資本時自界說處置 } @Override public void onError(WebView w包養網pptebView, ResourceRequest request, ResourceError error) { super.onError(webView, request, error); // 產生過錯時自界說處置 } }); webView.setBrowserAgent(new BrowserAgent(SimpleWebViewAbilitySlice.this) { @Override public void onTitleUpdated(WebView webView, String title) { super.onTitleUpdated(webView, title); // 題目變革時自界說處置 } @Override public void onProgressUpdated(WebView webView, int newProgress) { super.onProgressUpdated(webView, newProgress); if (newProgress FinalValue.PROGRESS_BAR_FINISHED) { otherWebViewProgressBar.setVisibility(Component.VISIBLE); otherWebViewProgressBar.setProgressValue(newProgress); } else if (newProgress == FinalValue.PROGRESS_BAR_FINISHED) { otherWebViewProgressBar.setVisibility(Co包養pttmponent.HIDE); } // 加載進度變革時自界說處置 } }); } break; case ResourceTable.Id_button_webview_back: { if (webView.getNavigator().canGoBack()) { webView.getNavigator().goBack(); } } break; case ResourceTable.Id_button_webview_refresh: { webView.reload(); } break; case ResourceTable.Id_button_webview_forward: { if (webView.getNavigator().canGoForward()) { webView.getNavigator().goForward(); } } break; default: { System.out.println( quot;沒有選擇任何的頁面“誰告訴你的?你的祖母?”她苦笑著問道,喉嚨裡又湧出一股血熱,讓她咽了下去,才吐了出來。 quot;); } break; } } }; /** * @Author JavaAIer * @Description : * @Date: 2021/4/16 14:46 * * @param intent */ @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_simple_web_view); otherWebViewProgressBar = (ProgressBar) findComponentById(ResourceTable.Id_other_webView_progressBar); textWebViewUrl = (TextField) findComponentById(ResourceTable.Id_text_webView_Url); buttonWebViewSurf = (Button) findComponentById(ResourceTable.Id_button_webview_surf); buttonWebViewSurf.setClickedListener(clickedListener); buttonWebViewBack = (Button) findComponentById(ResourceTable.Id_button_webview_back); buttonWebViewBack.setClickedListener(clickedListener); buttonWebViewRefresh = (Button) findComponentById(ResourceTable.Id包養合約_button_webview_refresh); buttonWebViewRefresh.setClickedListener(clickedListener); buttonWebViewForward = (Button) findComponentById(ResourceTable.Id_button_webview_forward); buttonWebViewForward.setClickedListener(clic包養網單次kedListener); webView = (WebView) findComponentById(ResourceTable.Id_webview_webview_webview); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}/** 這一截卡哇伊年夜喵在config.json用了,我發明用不消沒啥差別啊
* https://blog.csdn.net/qq_33259323/article/details/115596296
* quot;default quot;: { quot;network quot;: { quot;cleartextTraffic quot;: true, quot;securityConfig quot;: { 短期包養 quot;domainSett包養appings quot;: {
quot;cleartextPermitted quot;: true, quot;domains quot;: [ { quot;subdomains quot;: true, quot;name quot;: quot;www.harmonyos.com quot; } ] } } } }** */
config.json
{
quot;app quot;: { quot;bundleName quot;: quot;com.javaaier.family.huawei包養一個月 quot;, quot;vendor quot;: quot;javaaier quot;, quot;version quot;: { quot;code quot;: 1, quot;name quot;: quot;1.0 quot; }, quot;apiVersion quot;: { quot;compatible quot;: 5, quot包養妹;target quot;: 5, quot;releaseType quot;: quot;Beta1 quot; } }, quot;deviceConfig quot;: { }, quot;module quot;: { quot;package quot;: quot;com.javaaier.family.huawei quot;, quot;name quot;: quot;.MyApplication quot;, quot;deviceType quot;: [ quot;phone quot; ], quot;distro quot;: { quot;deliveryWithInstall quot;: true, quot;moduleName quot;: quot;entry q包養心得uot;, quot;moduleType quot;: quot;entry quot; }, quot;abilities quot;: [ { quot;skills quot;: [ { quot;entities quot;: [包養價格 quot;entity.system.home quot; ], quot;actions quot;: [ quot;action.system.home quot; ] } ], quot;orientation quot;: quot;unspecified quot;, quot;name quot;: quot;com.javaaier.family.huawei.MainAbility quot;, quot;icon quot;: quot;$media:icon quot;, quot;description quot;: quot;$string:mainability_description quot;, quot;label quot;: quot;$string:app_name quot;, quot;type quot;: quot;page quot;, quot;launchType quot;: quot;standard quot; }, { quot;orientation quot;: quot;unspecif包養網比較ied quot;, quot;name 包養網站quot;: quot;com.javaaier.family.huawei.SimpleWebViewAbility quot;, quot;icon quot;: quot;$media:icon quot;, quot;description quot;: quot;$string:simplewebviewability_description quot;, quot;label quot;: quot;$string:app_name quot;, 包養網評價 quot;type quot;: quot;page quot;, quot;launchType quot;: quot;standard quot; } ], quot;reqPermissions quot;: [ { quot;name quot;: quot;ohos.permission.INTERNET quot; } ] }}
`
發佈留言