WebView的简单入门

(1)使用WebView则需要在布局文件中使用相应的布局控件

布局文件如下:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/web_title_relativelayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignLeft="@+id/web_title_relativelayout" >

        <Button
            android:id="@+id/back"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:text="返回" />

        <TextView
            android:id="@+id/tv_url"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="TextView" />

        <Button
            android:id="@+id/refresh"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="刷新" />
    </RelativeLayout>
    <!-- WebView的使用 -->

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/web_title_relativelayout" />

</RelativeLayout>
MainActivity.java的代码如下:

package com.lc.webview_test1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 使用WebView要添加使用网络的权限
 */
public class MainActivity extends Activity implements OnClickListener {

	private WebView webView;
	private Button backButton;
	private Button refreshButton;
	private TextView tv_url;

	private static final String URL_VALUE = "http://www.baidu.com/";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		webView = (WebView) this.findViewById(R.id.webView1);
		backButton = (Button) this.findViewById(R.id.back);
		refreshButton = (Button) this.findViewById(R.id.refresh);
		tv_url = (TextView) this.findViewById(R.id.tv_url);
		// 设置监听事件
		backButton.setOnClickListener(this);
		refreshButton.setOnClickListener(this);

		// 如果只是用这句话的话,这样的话默认的加载系统自带的游览器打开该url
		// webView.loadUrl("http://www.baidu.com/");

		webView.loadUrl(URL_VALUE);
		/*
		 * 为避免使用系统自带的游览器,我们需要重写一下的方法
		 */
		webView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onReceivedTitle(WebView view, String title) {
				tv_url.setText(title);// 设置标题
				super.onReceivedTitle(view, title);
			}
		});

		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return super.shouldOverrideUrlLoading(view, url);
			}

			/**
			 * 获得异常的界面,展示错误页面的
			 */
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);
				view.loadUrl("file:///android_asset/404.html");
			}
		});

	}

	/**
	 * 按钮点击事件的操作方法
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			Toast.makeText(MainActivity.this, "back...", 1).show();
			finish();
			break;
		case R.id.refresh:
			Toast.makeText(MainActivity.this, "refresh...", 1).show();
			webView.reload();// 重新加载
			break;
		default:
			break;
		}
	}
}

演示效果如下:



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐刘根

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值