webview的一般使用(主要是避免使用手机自身浏览器加载、webview内容显示的自适应、webview显示中对传入url的处理)
loadWebview.setVisibility(View.VISIBLE);
//是否可以缩放
loadWebview.getSettings().setSupportZoom(true);
//表示自适应屏幕尺寸
loadWebview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
loadWebview.getSettings().setLoadWithOverviewMode(true);
//设置client
loadWebview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//这里的return的含义:true表示对这个url的处理交给开发者自行处理;return false表示这个事件依然交给webview来处理
if (url.contains("bai")) {
loadWebview.loadUrl("https://segmentfault.com/q/1010000004896607");
Toast.makeText(getApplicationContext(), "百度", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().toString().contains("bai")) {
loadWebview.loadUrl("https://segmentfault.com/q/1010000004896607");
Toast.makeText(getApplicationContext(), "百度", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});
loadWebview.loadUrl("http://www.baidu.com");
progress.setVisibility(View.VISIBLE);
JS交互: Java代码:
public class MainActivity extends AppCompatActivity {
private WebView contentWebView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentWebView = (WebView) findViewById(R.id.webview);
// 启用javascript
contentWebView.getSettings().setJavaScriptEnabled(true);
// 从assets目录下面的加载html
contentWebView.loadUrl("file:///android_asset/web.html");
contentWebView.addJavascriptInterface(MainActivity.this,"android");
//无参调用Js点击
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 无参数调用
contentWebView.loadUrl("javascript:javacalljs()");
}
});
//有参调用Js点击
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 传递参数调用
contentWebView.loadUrl("javascript:javacalljswith(" + "'http://blog.csdn.net/Leejizhou'" + ")");
}
});
}
//由于安全原因 需要加 @JavascriptInterface
@JavascriptInterface
public void startFunction(){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"show",3000).show();
}
});
}
@JavascriptInterface
public void startFunction(final String text){
runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(MainActivity.this).setMessage(text).show();
}
});
}
}
html代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<script type="text/javascript">
function javacalljs(){
document.getElementById("content").innerHTML =
"<br\>JAVA调用了JS的无参函数";
}
function javacalljswith(arg){
document.getElementById("content").innerHTML =
("<br\>"+arg);
}
</script>
</head>
<body>
HTML 内容显示 <br/>
<h1><div id="content">内容显示</div></h1>
<br/>
<input type="button" value="点击调用java代码" οnclick="window.android.startFunction()" />
<br/>
<input type="button" value="点击调用java代码并传递参数" οnclick="window.android.startFunction('http://blog.csdn.net/Leejizhou')" />
</body>
</html>