webview 类似浏览器


 

package com.lilin.browser;

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

public class Browser extends Activity implements OnClickListener {
	WebView webView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.main);
		init();

		webView.setWebChromeClient(new WebChromeClient() { // 为WebView设置WebChromeClient
					@Override
					public void onProgressChanged(WebView view, int newProgress) {// 重写onProgressChanged方法
						Browser.this.setProgress(newProgress * 100);
					}
				});

		webView.setWebViewClient(new WebViewClient() { // 为WebView设置WebViewClient
					public void onReceivedError(WebView view, int errorCode,
							String description, String failingUrl) {// 重写onReceivedError方法

						Toast.makeText(Browser.this, "Sorry!" + description,
								2000).show();

					}
				});
	}

	private void init() {
		webView = (WebView) findViewById(R.id.wv);/* webview的设置 */
		Button btn = (Button) findViewById(R.id.btn); // 获取Button对象
		// 设置前进后退按钮
		Button btnForward = (Button) findViewById(R.id.btnForward);
		Button btnBack = (Button) findViewById(R.id.btnBack);
		btn.setOnClickListener(this);
		btnForward.setOnClickListener(this);
		btnBack.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn:
			if (webView.canGoForward()) { // 判断是否能够前进
				webView.goForward();
			} else {
				Toast.makeText(Browser.this, "对不起,您现在不能前进!", 2000).show();
			}
			break;
		case R.id.btnForward:
			if (webView.canGoForward()) { // 判断是否能够前进
				webView.goForward();
			} else {
				Toast.makeText(Browser.this, "对不起,您现在不能前进!", 2000).show();
			}
			break;
		case R.id.btnBack:
			if (webView.canGoBack()) { // 判断是否能够前进
				webView.goBack();
			} else {
				Toast.makeText(Browser.this, "对不起,您现在不能后退!", 2000).show();
			}
			break;

		default:
			break;
		}
	}
}

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="horizontal" android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<Button android:id="@+id/btnForward" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="@string/forward" />
		<Button android:id="@+id/btnBack" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="@string/back" />
		<EditText android:id="@+id/et" android:layout_width="150dp"
			android:layout_height="wrap_content" android:singleLine="true"
			android:selectAllOnFocus="true" android:hint="@string/et" />
		<Button android:id="@+id/btn" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:layout_gravity="right"
			android:text="@string/btn" />
	</LinearLayout>
	<WebView android:id="@+id/wv" android:layout_width="wrap_content"
		android:layout_height="fill_parent" />
</LinearLayout>

 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值