package com.example.android_webview_shouldoverrideurlloading;
import android.graphics.Bitmap;
import android.inputmethodservice.Keyboard.Key;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
/**
* 添加 权限 <uses-permission android:name="android.permission.INTERNET"/>
*
* @author Administrator
*
*/
public class MainActivity extends Activity {
private WebView webView;
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
// webView.setBackgroundColor(0);//先设置背景色为transparent
// webView.setBackgroundResource(R.drawable.ic_launcher);//然后设置背景图片
webView.setWebViewClient(new WebViewClient() {
/**
* 2. 当前网页的链接仍在webView中跳转
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});
// 3. 页面加载进度
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
setTitle("页面加载中,请稍候..." + newProgress + "%");
setProgress(newProgress * 100);
if (newProgress == 100) {
setTitle(R.string.app_name);
}
super.onProgressChanged(view, newProgress);
}
});
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(MainActivity.this, "Oh no! " + description,
Toast.LENGTH_SHORT).show();
}
// 开始
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
Toast.makeText(MainActivity.this, "-->>onPageStarted", 1)
.show();
}
// 加载完毕
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
Toast.makeText(MainActivity.this, "-->>onPageFinished", 1)
.show();
}
/*
* Notify the host application that the WebView will load the
* resource specified by the given url.
*
* Parameters view The WebView that is initiating the callback. url
* The url of the resource the WebView will load.
*
* (non-Javadoc)
*
* @see
* android.webkit.WebViewClient#onLoadResource(android.webkit.WebView
* , java.lang.String)
*/
@Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
Toast.makeText(MainActivity.this, "-->>onLoadResource", 1)
.show();
}
});
// 加载Url
webView.loadUrl("http://www.baidu.com");
}
// 1. back键控制网页后退
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (webView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
75 Android WebView (五)
最新推荐文章于 2023-09-11 20:01:35 发布