Android實用筆記——使用WebView在界面中顯示網頁

一、經過Intent調用系統瀏覽器   html

package com.example.myandroidwebview;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {
	//聲明地址
	private String url="http://2014.qq.com/";
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //一、經過Intent調用系統瀏覽器
        Uri uri=Uri.parse(url);//url爲你要連接的地址
        Intent intent =new Intent(Intent.ACTION_VIEW,uri);
        startActivity(intent);
    }
    
}

 

二、使用WebView顯示網頁java

        將WebView加入應用android

        要在應用中加入WebView,須要在活動佈局中加入<WebView>元素web

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

        在AndroidManifest。xml文件中添加網絡訪問的權限瀏覽器

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET"/>

        編輯MainActivity.xml文件網絡

package com.example.myandroidwebview;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {
	//一、聲明WebView
	private WebView webView;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web);

        
        //二、創建初始化函數
        init();
    }


	private void init() {
		// TODO Auto-generated method stub
		webView=(WebView) findViewById(R.id.webView);
		//三、去AndroidManifest。xml文件中添加網絡訪問的權限
		//四、經過webView的LoadUrl的函數去加載兩種類型的頁面,一種是本地文件,一種是網頁
		//WebView打開本地資源這裏是三個斜槓,必定注意
		//webView.loadUrl("file:///android_asset/example.html");
		//WebView加載Web資源
		webView.loadUrl("http://www.baidu.com");
	}
    
}

 

三、使用WebView直接顯示頁面app

    當用戶點擊一個WebView中的頁面連接時,一般是由默認的瀏覽器打開並加載目標URL的。然而,你能夠在WebView中覆蓋這一行爲,那麼連接就會在WebView中打開。ide

private void init() {
		// TODO Auto-generated method stub
		webView=(WebView) findViewById(R.id.webView);
		//三、去AndroidManifest。xml文件中添加網絡訪問的權限
		//四、經過webView的LoadUrl的函數去加載兩種類型的頁面,一種是本地文件,一種是網頁
		//WebView打開本地資源這裏是三個斜槓,必定注意
		webView.loadUrl("file:///android_asset/example.html");
		//WebView加載Web資源
		//webView.loadUrl("http://www.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;
			}
			//WebViewClient幫助WebView去處理一寫頁面控制和請求通知
			
		});
	}

        

四、在WebView中使用JavaSript函數

    若是想要加載在WebView中的web頁面使用JavaScript,就須要在WebView中經過WebSettings啓用JavaScript。咱們能夠經過getSettings()來獲取WebSettings的值,而後經過setJavaScriptEnabled()來啓用JavaScript。佈局

相關文章
相關標籤/搜索