1.布局
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
2.代码
private void initWebView() {
WebView webview = findViewById(R.id.webview);
//1. Url的地址
String mUrl = "http://www.baidu.com/";
//2. 要提交的数据
String mData = "name=李图&age=18&sex=n&page=1";
WebSettings webSettings = webview.getSettings();
//3. 设置缓存
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//4. 适应分辨率
webSettings.setUseWideViewPort(true);
//5. 设置能够解析Javascript
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setDomStorageEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.setHapticFeedbackEnabled(false);
//6. java回调js代码,不要忘了@JavascriptInterface这个注解,不然点击事件不起作用
webview.addJavascriptInterface(new JsInterface(this), "android");
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
if (("app/appGetCashCallback.html?source=m").equals(uri.toString())) {
finish();
startActivityNoResult(TakeMoneySuccessActivity.class, null);
}
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//加载完毕,关闭加载的dialog
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//接受信任所有网站的证书
handler.proceed();
}
});
//7. 表单提交,postUrl
webview.postUrl(mUrl,mData.getBytes());
}
版权声明:本文为JiYaRuo原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。