要是webview能夠與JavaScript互動,首先需要webview要啟用JavaScript:
- WebSettings webSettings = myWebView.getSettings();
- webSettings.setJavaScriptEnabled(true); 然後建立JavaScript的介面:
- public class WebAppInterface {
- Context mContext;
-
- /** Instantiate the interface and set the context */
- WebAppInterface(Context c) {
- mContext = c;
- }
-
- /** Show a toast from the web page */
- @JavascriptInterface
- public void showToast(String toast) {
- Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
- }
- }
給webview添加JavaScript介面:
[html] view plaincopy
- myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
本地JavaScript檔案:
[javascript] view plaincopy
-
-
- <script type="text/javascript">
- function showAndroidToast(toast) {
- Android.showToast(toast);
- }
- </script>
整個代碼如下:
[java] view plaincopy
- public class MainActivity extends Activity {
- private WebView myWebView;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- myWebView = (WebView) findViewById(R.id.webview);
- WebSettings webSettings = myWebView.getSettings();
- webSettings.setJavaScriptEnabled(true);
- myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
- ProcessWebString();
-
- }
-
- public class WebAppInterface {
- Context mContext;
-
- /** Instantiate the interface and set the context */
- WebAppInterface(Context c) {
- mContext = c;
- }
-
- /** Show a toast from the web page */
- @JavascriptInterface
- public void showToast(String toast) {
- Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
- }
- }
-
- private void ProcessWebString() {
- // 載入 asset 檔案
- String tpl = getFromAssets("web_tpl.html");
- myWebView.loadDataWithBaseURL(null, tpl, "text/html", "utf-8", null);
- }
-
- /*
- * 擷取html檔案
- */
- public String getFromAssets(String fileName) {
- try {
- InputStreamReader inputReader = new InputStreamReader(
- getResources().getAssets().open(fileName));
- BufferedReader bufReader = new BufferedReader(inputReader);
- String line = "";
- String Result = "";
- while ((line = bufReader.readLine()) != null)
- Result += line;
- return Result;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
-
- } 這裡擷取到的是assert檔案下的html字串,寫了一個方法擷取assert檔案下html
還有種方法可以直接load
webView.loadUrl("file:///android_asset/html/company.html");
assets檔案下的html檔案下的company.html檔案
webView.setWebChromeClient(new MyWebClient());
private class MyWebClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub
Intent intent = null;
if (message.contains("tid")) {
Pattern pattern = Pattern.compile("tid=(\\d+)");
Matcher matcher = pattern.matcher(message);
String tid = "";
while (matcher.find()) {
String st = matcher.group(0);
tid = st.split("=")[1];
System.out.println("---" + tid);
}
intent = new Intent(BbsMyHomeActivity.this,
BbsHomeDetailTestActivity.class);
intent.putExtra("tid", tid);
} else {
intent = new Intent(BbsMyHomeActivity.this,
MyAttentionDetailActivity.class);
intent.putExtra("uid", message);
}
startActivity(intent);
// new CustomeToast(BbsMyHomeActivity.this, message);
new PopupToast(BbsMyHomeActivity.this, message, footlayout);
// 少寫了這行代碼可能會導致,點擊了一次js裡的alert彈出方法,無法點擊第二次。
result.cancel();
return true;
}
}
http://blog.csdn.net/ithomer/article/details/8737999
做項目的時候還發現一個問題,就是WebView的addJavascriptInterface方法失效的問題
裡面的類的方法名上面要加annotation
@JavascriptInterface
表忘了導包import android.webkit.JavascriptInterface;