安卓学习-WebView
生活随笔
收集整理的這篇文章主要介紹了
安卓学习-WebView
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
?
?
WebView與JS交互
<uses-permission android:name="android.permission.INTERNET"></uses-permission>activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.webview.MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView">
</WebView>
</RelativeLayout>
MainActivity.java
package com.example.administrator.webview;import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
handler = new Handler();
// webView.loadData("<html><title></title><body>hello webView</body></html>","text/html","utf-8");
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);//允許使用JS
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);//允許縮放
webView.requestFocus();//防止軟鍵盤無法打開的問題
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
//設置點擊鏈接在當前WebView中顯示
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
//處理標題,圖標等等
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
});
webView.addJavascriptInterface(new MyObject(), "demo");
//webView.loadUrl("http://www.baidu.com");
webView.loadUrl("file:///android_asset/index.html");
}
public class MyObject
{
@JavascriptInterface
public void clickOnAndroid() {
handler.post(new Runnable() {
@Override
public void run() {
webView.loadUrl("javascript:wave()");
}
});
}
}
//設置回退鍵
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
index.xml
<html><title>hello webView</title>
<script language="javascript">
<!--
function wave()
{
document.getElementById("imgid").src="pic0.jpg";
}
//->
</script>
<body>
<a onClick="window.demo.clickOnAndroid()">
<img src="pic15.jpg" id="imgid"/>
</a>
</body>
</html>
轉載于:https://my.oschina.net/xiaofeiandroid/blog/655495
總結
以上是生活随笔為你收集整理的安卓学习-WebView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS开发如何在iTunes中查看沙河下
- 下一篇: Mac下好用的批量文件扩展名修改工具