首先添加权限:
布局文件:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:id="@+id/preNums"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="当前进度:0%" />
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/preNums" />
html5页面代码:
手机网页*{ padding:0px; margin:0px;}
div{ margin:0px auto;}
.mytop{ height:60px; background-color:#000000; text-align:center;}
.myfont{ color:#FFFFFF; font-size:16px; font-weight:bold; line-height:60px;}
.mytest{ background-color:#FF0000; margin:20px auto; width:100px; height:100px; border-radius:50px; box-shadow:8px 8px 8px #0000FF; line-height:100px; text-align:center;}
function ChangeDiv(){
//document.getElementById("insertDiv").innerHTML='哈哈';
//调用java代码
MyAndroid.MyFun("哈哈可以调用");
}
// 被java调用的方法
function ToJavaFun(outstr){
document.getElementById("insertDiv").innerHTML=outstr;
}
java代码:
package com.example.mywebview;
import android.os.Bundle;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;
/*
* author:future
*
* sijienet.com
*
* */
@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
public class MainActivity extends Activity {
WebView webView;
TextView textView;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
webView = (WebView) findViewById(R.id.webView1);
textView = (TextView) findViewById(R.id.preNums);
// 开启js接口模式
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyAndroidFun(), "MyAndroid");
webView.setWebViewClient(new MyWebClient());
webView.setWebChromeClient(new MyChrome());// 配置默认参数
webView.loadUrl("file:///android_asset/index.html");
}
class MyChrome extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// 进度更新
textView.setText("当前进度:" + newProgress + "%");
super.onProgressChanged(view, newProgress);
}
}
class MyAndroidFun {// 调用类作用于
@JavascriptInterface
public void MyFun(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT)
.show();
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
String outString = "我的内容";
webView.loadUrl("javascript:ToJavaFun('" + outString + "')");
}
});
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
if (webView.canGoBack()) {
webView.goBack();
}
// super.onBackPressed();
}
class MyWebClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
if (url != null && url.length() > 0) {
if (url.indexOf("http://") != -1
|| url.indexOf("https://") != -1) {// 不存在
} else {
// 添加前缀
url = "http://" + url;
}
view.loadUrl(url);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
}
}