赞
踩
在AndroidManifest.xml设置访问网络权限:
<span style="font-size:24px;"><span style="font-size:24px;"><uses-permission android:name="android.permission.INTERNET"/></span></span>
<span style="font-size:24px;"><span style="font-size:24px;"><WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView" /></span></span>
<span style="font-size:24px;"> mWebView=(WebView) findViewById(R.id.webView); mWebView.setVisibility(View.VISIBLE); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mWebView.loadUrl("http://www.baidu.com"); </span>
方法是使用setWebViewClient
<span style="font-size:24px;">import android.app.Activity;import android.os.Bundle;import android.webkit.WebView;import android.webkit.WebViewClient; public class MainActivity extends Activity {private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); init(); } private void init(){ webView = (WebView) findViewById(R.id.webView); //WebView加载web资源 webView.loadUrl("http://baidu.com"); //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url); return true; } }); } }</span>
<span style="font-size:24px;">//启用支持javascriptWebSettings settings = webView.getSettings();settings.setJavaScriptEnabled(true);</span>
<span style="font-size:24px;">//改写物理按键——返回的逻辑 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode==KeyEvent.KEYCODE_BACK) { if(webView.canGoBack()) { webView.goBack();//返回上一页面 return true; } else { System.exit(0);//退出程序 } } return super.onKeyDown(keyCode, event); }</span>
<span style="font-size:24px;">webView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub if (newProgress == 100) { // 网页加载完成 } else { // 加载中 } } });</span>
WebChromeClient是辅助WebView处理Javascript的对话框,网站图标,网站title,加载进度等 onCloseWindow(关闭WebView) onCreateWindow() onJsAlert (WebView上alert是弹不出来东西的,需要定制你的WebChromeClient处理弹出) onJsPrompt onJsConfirm onProgressChanged onReceivedIcon onReceivedTitle 比如可以添加进度条,使得界面更友好
<span style="font-size:24px;">webview1.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { setProgress(progress * 100); if(progress == 100){ imageView1.setVisibility(View.GONE); tv1.setVisibility(View.GONE); pb1.setVisibility(View.GONE); fy1.setVisibility(View.GONE); } } } ); </span>
<span style="font-size:24px;">webview1.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Handle the error Toast.makeText(getApplicationContext(), "网络连接失败 ,请连接网络。", Toast.LENGTH_SHORT).show();} public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); //设置字符集编码 webview1.getSettings().setDefaultTextEncodingName("UTF-8"); webview1.loadUrl("http://www.android100.org/");</span>