WebView是Android中一個(gè)非常實(shí)用的組件,它和Safai、Chrome一樣都是基于Webkit網(wǎng)頁(yè)渲染引擎,可以通過(guò)加載HTML數(shù)據(jù)的方式便捷地展現(xiàn)軟件的界面,下面通過(guò)本文給大家介紹Webview實(shí)現(xiàn)android簡(jiǎn)單的瀏覽器實(shí)例代碼。
實(shí)現(xiàn)了瀏覽器的返回 前進(jìn) 主頁(yè) 退出 輸入網(wǎng)址的功能

注釋的很清楚啦 就不多說(shuō)了
首先是布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/et_url" android:layout_width="320dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_login" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="登錄" /> </LinearLayout> <WebView android:layout_weight="2" android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <LinearLayout android:layout_weight="7.5" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:background="#000000" > <Button android:id="@+id/btn_back" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="←" /> <Button android:id="@+id/btn_menu" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="主頁(yè)" /> <Button android:id="@+id/btn_forward" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="→" /> <Button android:id="@+id/btn_exit" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="exit" /> </LinearLayout> </LinearLayout>
MainActivity
package com.example.webview; import android.os.Bundle; import android.app.Activity; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener { private String url = null; private WebView webView; private EditText et_url; private Button btn_login; private Button btn_back; private Button btn_exit; private Button btn_forward; private Button btn_menu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 窗口進(jìn)度條 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); setProgressBarIndeterminate(true); webView = (WebView) findViewById(R.id.webView); et_url = (EditText) findViewById(R.id.et_url); btn_login = (Button) findViewById(R.id.btn_login); btn_back = (Button) findViewById(R.id.btn_back); btn_exit = (Button) findViewById(R.id.btn_exit); btn_forward = (Button) findViewById(R.id.btn_forward); btn_menu = (Button) findViewById(R.id.btn_menu); // 對(duì)五個(gè)按鈕添加點(diǎn)擊監(jiān)聽(tīng)事件 btn_login.setOnClickListener(this); btn_back.setOnClickListener(this); btn_exit.setOnClickListener(this); btn_forward.setOnClickListener(this); btn_menu.setOnClickListener(this); } // btn_login的觸發(fā)事件 點(diǎn)擊后 webView開(kāi)始讀取url protected void startReadUrl(String url) { // TODO Auto-generated method stub // webView加載web資源 webView.loadUrl(url); // 覆蓋webView默認(rèn)通過(guò)系統(tǒng)或者第三方瀏覽器打開(kāi)網(wǎng)頁(yè)的行為 // 如果為false調(diào)用系統(tǒng)或者第三方瀏覽器打開(kāi)網(wǎng)頁(yè)的行為 webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub // webView加載web資源 view.loadUrl(url); return true; } }); // 啟用支持javascript WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); // web加載頁(yè)面優(yōu)先使用緩存加載 settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // 當(dāng)打開(kāi)頁(yè)面時(shí) 顯示進(jìn)度條 頁(yè)面打開(kāi)完全時(shí) 隱藏進(jìn)度條 webView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub setTitle("本頁(yè)面已加載" + newProgress + "%"); if (newProgress == 100) { closeProgressBar(); } else { openProgressBar(newProgress); } super.onProgressChanged(view, newProgress); } }); } // 打開(kāi)進(jìn)度條 protected void openProgressBar(int x) { // TODO Auto-generated method stub setProgressBarIndeterminateVisibility(true); setProgress(x); } // 關(guān)閉進(jìn)度條 protected void closeProgressBar() { // TODO Auto-generated method stub setProgressBarIndeterminateVisibility(false); } // 改寫(xiě)物理按鍵 返回鍵的邏輯 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { if (webView.canGoBack()) { // 返回上一頁(yè)面 webView.goBack(); return true; } else { // 退出程序 finish(); } } return super.onKeyDown(keyCode, event); } // 對(duì)按鈕事件的處理 @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_login: url = "http://" + et_url.getText().toString(); url = url.replace(" ", ""); startReadUrl(url); break; case R.id.btn_back: if (webView.canGoBack()) { webView.goBack(); } else { finish(); } break; case R.id.btn_forward: if (webView.canGoForward()) { webView.goForward(); } break; case R.id.btn_exit: finish(); break; case R.id.btn_menu: startReadUrl("http://www.baidu.com"); break; } } }最后不要忘記在AndroidManifest.xml文件中配置網(wǎng)絡(luò)訪問(wèn)的權(quán)限
<uses-permission android:name="android.permission.INTERNET"/>
以上內(nèi)容給大家介紹了Webview實(shí)現(xiàn)android簡(jiǎn)單的瀏覽器實(shí)例代碼,希望對(duì)大家有所幫助!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注