AndroidWebView组件-简易浏览器

AndroidWebView组件-简易浏览器

AndroidWebView组件

  • 权限

<uses-permission android:name="android.permission.INTERNET" />

  • 视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical"
	tools:context=".MainActivity">

	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:gravity="center_horizontal"
		android:orientation="vertical">

		<ProgressBar
			style="?android:attr/progressBarStyleHorizontal"
			android:layout_height="2dp"
			android:layout_width="match_parent"
			android:id="@+id/mainProgressBar1"/>

	</LinearLayout>

	<WebView
		android:id="@+id/web"
		android:layout_width="match_parent"
		android:layout_height="0dp"
		android:layout_weight="1"
		android:animateLayoutChanges="true"
		android:background="#00840E"/>

</LinearLayout>
  • MainActivity
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.webkit.*;
import android.widget.*;

public class MainActivity extends Activity {
	private WebView mWebView;
	private ProgressBar mProgressBar;
	private String httpurl = "http://m.baidu.com";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		

		// 网页加载进度条
		mProgressBar = (ProgressBar) findViewById(R.id.mainProgressBar1);


		mWebView = (WebView) findViewById(R.id.web);

		// 使WebView获得焦点
		mWebView.setFocusable(true);
		mWebView.setFocusableInTouchMode(true);
		mWebView.requestFocus();
		mWebView.requestFocusFromTouch();
		// WebView加载web资源
		mWebView.loadUrl(httpurl);
		// 覆盖WebView默认通过第三方或系统默认浏览器打开网页的行为,使网页可以在WebView中打开
		mWebView.setWebViewClient(new WebViewClient());

	
		//允许WebView访问JavaScript:
		mWebView.getSettings().setJavaScriptEnabled(true);
		//为WebView绑定一个定制的JavaScript接口
		mWebView.addJavascriptInterface(new WebAppInterface(getApplicationContext()), "Android");
		// WebView加载网页优先使用缓存加载
		mWebView.getSettings()
			.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

		// 设置进度条显示
		mWebView.setWebChromeClient(new WebChromeClient() {
				@Override
				public void onProgressChanged(WebView view, int newProgress) {
					if (newProgress < 100) {
						mProgressBar.setProgress(newProgress);
					} else {
						mProgressBar.setProgress(0);
					}
					super.onProgressChanged(view, newProgress);
				}
			});
	}

	
	static class WebAppInterface {

        private Context mContext;

        public WebAppInterface(Context context) {
            mContext = context;
        }

        ;

        @JavascriptInterface
        public void showToast(String toast) {
            Toast.makeText(mContext.getApplicationContext(), toast, Toast.LENGTH_LONG).show();
        }
    }
	
	
	
	// 改写物理按键————返回的逻辑
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果点击返回键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 如果有上一页
			if (mWebView.canGoBack()) {
				mWebView.goBack();
				return true;
			} else {
				System.exit(0);
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}