android:webview實現簡單的瀏覽器
實現了瀏覽器的返回 前進 首頁 退出 輸入網址的功能
注釋的很清楚啦 就不多說了
首先是布局檔案
<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="登入"> <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"> </linearlayout></webview></button><button android:id="@+id/btn_back" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="←"></button><button android:id="@+id/btn_menu" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="首頁"></button><button android:id="@+id/btn_forward" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="→"></button><button android:id="@+id/btn_exit" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="exit"></button></edittext></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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 視窗進度條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);// 對五個按鈕添加點擊監聽事件btn_login.setOnClickListener(this);btn_back.setOnClickListener(this);btn_exit.setOnClickListener(this);btn_forward.setOnClickListener(this);btn_menu.setOnClickListener(this);}// btn_login的觸發事件 點擊後 webView開始讀取urlprotected void startReadUrl(String url) {// TODO Auto-generated method stub// webView載入web資源webView.loadUrl(url);// 覆蓋webView預設通過系統或者第三方瀏覽器開啟網頁的行為// 如果為false調用系統或者第三方瀏覽器開啟網頁的行為webView.setWebViewClient(new WebViewClient() {@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {// TODO Auto-generated method stub// webView載入web資源view.loadUrl(url);return true;}});// 啟用支援javascriptWebSettings settings = webView.getSettings();settings.setJavaScriptEnabled(true);// web載入頁面優先使用緩衝載入settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// 當開啟頁面時 顯示進度條 頁面開啟完全時 隱藏進度條webView.setWebChromeClient(new WebChromeClient() {@Overridepublic void onProgressChanged(WebView view, int newProgress) {// TODO Auto-generated method stubsetTitle("本頁面已載入" + newProgress + "%");if (newProgress == 100) {closeProgressBar();} else {openProgressBar(newProgress);}super.onProgressChanged(view, newProgress);}});}// 開啟進度條protected void openProgressBar(int x) {// TODO Auto-generated method stubsetProgressBarIndeterminateVisibility(true);setProgress(x);}// 關閉進度條protected void closeProgressBar() {// TODO Auto-generated method stubsetProgressBarIndeterminateVisibility(false);}// 改寫物理按鍵 返回鍵的邏輯@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (keyCode == KeyEvent.KEYCODE_BACK) {if (webView.canGoBack()) {// 返回上一頁面webView.goBack();return true;} else {// 退出程式finish();}}return super.onKeyDown(keyCode, event);}// 對按鈕事件的處理@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (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檔案中配置網路訪問的許可權
<uses-permission android:name="android.permission.INTERNET"></uses-permission>