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。布局

相关文章
相关标签/搜索