網(wǎng)站首頁 編程語言 正文
前言
博主最近想做一款app,因?yàn)閮?nèi)容已經(jīng)有了,故想到了使用WebView來做?,現(xiàn)將代碼貼出如下,供有同樣需求的人參考,少走彎路
代碼如下
public class MainActivity extends Activity{ private WebView webview; private Handler handler; private ProgressDialog pd; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initWebView(); } /** * 初始化WebView容器 */ public void initWebView() { //實(shí)例化WebView對(duì)象 webview = new WebView(this); handler = new Handler() { public void handleMessage(Message msg) {//定義一個(gè)Handler,用于處理下載線程與UI間通訊 if (!Thread.currentThread().isInterrupted()) { switch (msg.what) { case 0: pd.show();//顯示進(jìn)度對(duì)話框 break; case 1: pd.hide();//隱藏進(jìn)度對(duì)話框,不可使用dismiss()、cancel(),否則再次調(diào)用show()時(shí),顯示的對(duì)話框小圓圈不會(huì)動(dòng)。 break; } } super.handleMessage(msg); } }; //設(shè)置WebViewClient webview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true;//如果不需要其他對(duì)點(diǎn)擊鏈接事件的處理返回true,否則返回false } @Override public void onPageFinished(WebView view, String url) { if (!webview.getSettings().getLoadsImagesAutomatically()) { webview.getSettings().setLoadsImagesAutomatically(true); } } }); webview.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int progress) { pd.setMessage("數(shù)據(jù)載入中("+progress+"%),請(qǐng)稍候!"); if (progress == 100) { handler.sendEmptyMessage(1);//如果全部載入,隱藏進(jìn)度對(duì)話框 } MainActivity.this.setProgress(progress); } }); pd = new ProgressDialog(MainActivity.this); pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); handler.sendEmptyMessage(0); WebSettings webSettings = webview.getSettings(); webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//優(yōu)先使用緩存的內(nèi)容 webSettings.setUseWideViewPort(true);//設(shè)定支持網(wǎng)頁viewport webSettings.setJavaScriptEnabled(true);//設(shè)置WebView屬性,能夠執(zhí)行Javascript腳本 // webSettings.setSupportZoom(true);//支持縮放 // webSettings.setBuiltInZoomControls(true);// 設(shè)置顯示縮放按鈕 // setZoomControlGone(webview); webSettings.supportMultipleWindows(); //多窗口 webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通過JS打開新窗口 webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//禁止左右滾動(dòng) webview.requestFocusFromTouch();//支持獲取手勢(shì)焦點(diǎn) webview.setHorizontalScrollBarEnabled(false);//水平不顯示滾動(dòng)條 webview.setVerticalScrollbarOverlay(true); getWindow().requestFeature(Window.FEATURE_PROGRESS);//設(shè)置窗口風(fēng)格為進(jìn)度條 //告訴WebView先不要自動(dòng)加載圖片,等頁面finish后再發(fā)起圖片加載 if (Build.VERSION.SDK_INT >= 19) { webview.getSettings().setLoadsImagesAutomatically(true); } else { webview.getSettings().setLoadsImagesAutomatically(false); } //加載需要顯示的網(wǎng)頁 webview.loadUrl("http://www.kailing.pub/juanmei/"); //設(shè)置Web視圖 setContentView(webview); } //實(shí)現(xiàn)放大縮小控件隱藏 public void setZoomControlGone(View view) { Class classType; Field field; try { classType = WebView.class; field = classType.getDeclaredField("mZoomButtonsController"); field.setAccessible(true); ZoomButtonsController mZoomButtonsController = new ZoomButtonsController(view); mZoomButtonsController.getZoomControls().setVisibility(View.GONE); try { field.set(view, mZoomButtonsController); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } //設(shè)置回退 //覆蓋Activity類的onKeyDown(int keyCoder,KeyEvent event)方法 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { webview.goBack(); return true; } else if (keyCode == KeyEvent.KEYCODE_BACK) { ConfirmExit();//按了返回鍵,但已經(jīng)不能返回,則執(zhí)行退出確認(rèn) return true; } return super.onKeyDown(keyCode, event); } /** * 退出確認(rèn)框 */ public void ConfirmExit() { AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this); ad.setTitle("退出"); ad.setMessage("是否退出娟妹美甲坊?"); ad.setPositiveButton("是", new DialogInterface.OnClickListener() {//退出按鈕 @Override public void onClick(DialogInterface dialog, int i) { // TODO Auto-generated method stub MainActivity.this.finish();//關(guān)閉activity System.exit(0); } }); ad.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { //不退出不用執(zhí)行任何操作 } }); ad.show();//顯示對(duì)話框 } // 繼承GestureListener,重寫left和right方法, private class MyGestureListener extends GestureListener { public MyGestureListener(Context context) { super(context); } @Override public boolean left() { webview.goForward(); return super.left(); } @Override public boolean right() { webview.goBack(); return super.right(); } } }
原文鏈接:http://www.kailing.pub/article/index/arcid/120.html
相關(guān)推薦
- 2022-03-17 Golang動(dòng)態(tài)調(diào)用方法小結(jié)_Golang
- 2022-10-06 Redis位圖bitmap操作_Redis
- 2022-06-01 Python學(xué)習(xí)之內(nèi)置函數(shù)總結(jié)_python
- 2022-11-15 Flutter異步操作實(shí)現(xiàn)流程詳解_Android
- 2022-09-26 Redis?哈希Hash底層數(shù)據(jù)結(jié)構(gòu)詳解_Redis
- 2022-06-16 golang?beego框架環(huán)境搭建過程_Golang
- 2023-02-26 C++?ROS與boost:bind()使用詳解_C 語言
- 2022-05-21 如何使用rust實(shí)現(xiàn)簡(jiǎn)單的單鏈表_相關(guān)技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支