Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android:webview實現簡單的浏覽器

android:webview實現簡單的浏覽器

編輯:關於android開發

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;

	@Override
	protected 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開始讀取url
	protected void startReadUrl(String url) {
		// TODO Auto-generated method stub
		// webView加載web資源
		webView.loadUrl(url);
		// 覆蓋webView默認通過系統或者第三方浏覽器打開網頁的行為
		// 如果為false調用系統或者第三方浏覽器打開網頁的行為
		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加載頁面優先使用緩存加載
		settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		// 當打開頁面時 顯示進度條 頁面打開完全時 隱藏進度條
		webView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				setTitle("本頁面已加載" + newProgress + "%");
				if (newProgress == 100) {
					closeProgressBar();
				} else {
					openProgressBar(newProgress);
				}
				super.onProgressChanged(view, newProgress);
			}
		});
	}

	// 打開進度條
	protected void openProgressBar(int x) {
		// TODO Auto-generated method stub
		setProgressBarIndeterminateVisibility(true);
		setProgress(x);
	}

	// 關閉進度條
	protected void closeProgressBar() {
		// TODO Auto-generated method stub
		setProgressBarIndeterminateVisibility(false);
	}

	// 改寫物理按鍵 返回鍵的邏輯
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (webView.canGoBack()) {
				// 返回上一頁面
				webView.goBack();
				return true;
			} else {
				// 退出程序
				finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	// 對按鈕事件的處理
	@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文件中配置網絡訪問的權限
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved