webView打开本地文件和使用第三方网页,以及使用自己的webViewClient

代码样例

package com.example.webview;





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




public class MainActivity extends Activity {
private WebView webView=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView=(WebView) findViewById(R.id.webView);
        
  /* 调用系统或第三方浏览器,打开uri   
   * 
   *   Uri uri=Uri.parse("http://www.baidu.com");
        Intent web=new Intent(Intent.ACTION_VIEW,uri);
        startActivity(web);*/
        //使用自己的webViewClient打开网页
        //需要在manifest文件中添加Internet权限
 /*       webView.loadUrl("http://www.qq.com");
        webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        return true;
        }
        });*/
        //路径规则file:///android_asset/文件名注意冒号后是三个‘/’,不要为我为神魔,我也不知道,反正累死我了,才调试出来
        //一定要注意不是android_assets而是android_asset
        webView.loadUrl("file:///android_asset/example.html");
        
    }


}

webView打开本地文件和使用第三方网页,以及使用自己的webViewClient_第1张图片

运行结果

webView打开本地文件和使用第三方网页,以及使用自己的webViewClient_第2张图片

你可能感兴趣的:(webView打开本地文件和使用第三方网页,以及使用自己的webViewClient)