
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>