WebView的简单使用

关于WebView的一些简单设置,直接上代码:

布局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.calendar.MainActivity"
    tools:ignore="MergeRootFrame" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <ProgressBar
        android:id="@+id/progressBar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100" />

</FrameLayout>

java代码:

public class MainActivity extends Activity {

	private ProgressDialog dialog;
	private WebView webView;
	private ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		progressBar = (ProgressBar) findViewById(R.id.progressBar);

		webView = (WebView) findViewById(R.id.webview);
		WebSettings settings = webView.getSettings();

		// 设置缩放允许
		settings.setBuiltInZoomControls(true);
		settings.setSupportZoom(true);

		// 设置初始比例
		// webView.setInitialScale(50);

		// 设置加载进来的页面自适应手机屏幕
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);

		// 设置使用内置浏览器打开
		webView.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// 加载页面时对话框提示
				if (dialog == null || !dialog.isShowing()) {
					dialog = new ProgressDialog(MainActivity.this);
					dialog.setMessage("正在加载中,请稍候...");
				}
				dialog.show();
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				dialog.dismiss();
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				super.shouldOverrideUrlLoading(view, url);
				view.loadUrl(url);
				return true;
			}
		});

		// 页面加载进度
		webView.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
				Message msg = new Message();
				msg.what = 200;
				msg.obj = newProgress;
				handler.sendMessage(msg);
			}
		});

		webView.loadUrl("http://www.zjut.edu.cn");
		// 重新加载,刷新
		// webView.reload();
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 200:
				int progress = (Integer) msg.obj;
				progressBar.setProgress(progress);
				if (progressBar.getProgress() == 100) {
					progressBar.setVisibility(View.GONE);
				}
				break;
			}
		};
	};

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_MOVE:
			if (webView.getScrollY() == 0) {
				// 滑动到顶部
				System.out.println("滑动到顶部.....");
			}
			if ((int) (webView.getContentHeight() * webView.getScale()) == (webView
					.getHeight() + webView.getScrollY())) {
				// 滑动到底部
				System.out.println("滑动到底部.....");
			}
			break;
		}
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 处理返回事件,如果页面还可以回退,则回退页面,否则finish
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (webView.canGoBack()) {
				webView.goBack();
			} else {
				finish();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}

相关推荐