一.Activity中使用
1.创建html文件(可以放在assest中)
这里是HTML
HTML内容显示
2.Activity_main.xml布局(只需要一个WebView控件即可)
3.MainActivity.java
1 public class MainActivity extends AppCompatActivity { 2 private WebView contentWebView = null; 3 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 contentWebView = (WebView) findViewById(R.id.webview);10 // 启用javascript11 contentWebView.getSettings().setJavaScriptEnabled(true);12 // 从assets目录下面的加载html13 contentWebView.loadUrl("file:///android_asset/b.html");14 contentWebView.addJavascriptInterface(MainActivity.this,"android");15 16 17 }18 19 //由于安全原因 需要加 @JavascriptInterface20 @JavascriptInterface21 public void startFunction(){22 23 runOnUiThread(new Runnable() {24 @Override25 public void run() {26 Toast.makeText(MainActivity.this,"Toast",Toast.LENGTH_SHORT).show();27 28 }29 });30 }31 32 @JavascriptInterface33 public void startFunction(final String text){34 runOnUiThread(new Runnable() {35 36 @Override37 public void run() {38 new AlertDialog.Builder(MainActivity.this).setMessage(text).show();39 40 }41 });42 43 44 }45 }
参考:
二.Fragment中使用
1 public View onCreateView(LayoutInflater inflater, ViewGroup container, 2 Bundle savedInstanceState) { 3 contentView = inflater.inflate(R.layout.fragment_web, container, false); 4 web_view = (WebView)contentView.findViewById(R.id.webview); 5 initView(); 6 web_view.loadUrl(url); 7 //web_view.loadUrl("file:///android_asset/Demo.html"); 8 web_view.setWebViewClient(new WebViewClient()); 9 10 mProgressBar = (ProgressBar)contentView.findViewById(R.id.progressbar);11 12 web_view.setWebChromeClient(new WebChromeClient(){13 @Override14 public void onProgressChanged(WebView view, int newProgress) {15 if (newProgress != 100) {16 mProgressBar.setProgress(newProgress);17 } else {18 mProgressBar.setVisibility(View.GONE);19 }20 }21 });22 23 24 25 // Inflate the layout for this fragment26 return contentView;27 }28 29 30 31 设置显示:32 33 private void initView() {34 WebSettings setting = web_view.getSettings();35 setting.setJavaScriptEnabled(true);//支持Js36 setting.setCacheMode(WebSettings.LOAD_DEFAULT);//缓存模式37 //是否支持画面缩放,默认不支持38 setting.setBuiltInZoomControls(true);39 setting.setSupportZoom(true);40 //是否显示缩放图标,默认显示41 setting.setDisplayZoomControls(false);42 //设置网页内容自适应屏幕大小43 setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);//注意网上例程很多的是.SINGLE_COLUMN,但调试时发现移动版网站会错位,所以改成44 //SINGLE_COLUMN45 46 setting.setUseWideViewPort(true);47 setting.setLoadWithOverviewMode(true);48 49 }
参考:
ps:在fragment中如果使用Activity中的js点击会出现点击事件不能执行的情况:此时需
1.(addJavascriptInterface方法中的第一个参数是一个类名,js要实现的方法必须写在类里面)
1 //Fragment中代码2 web_view = (WebView)mView.findViewById(R.id.webView);3 initView();4 web_view.loadUrl("http://hnzldzkj.cn/static/html/hh.html");5 web_view.addJavascriptInterface(new AndroidJs(),"android");
2.新建AndroidJs.java文件
1 package com.example.dell.zhidao; 2 3 import android.webkit.JavascriptInterface; 4 import android.widget.Toast; 5 6 public class AndroidJs { 7 @JavascriptInterface 8 public void startFunction(){ 9 System.out.println("JSFDSFSDFDSFDSF");10 }11 }
2.或者
1 //对js点击事件的实现 2 @JavascriptInterface 3 public void startFunction(final String ids){ 4 this.getActivity().runOnUiThread(new Runnable() { 5 @Override 6 public void run() { 7 getActivity().setContentView(R.layout.niday); 8 } 9 });10 }