Using jqMath in Android application

巧了我就是萌 提交于 2019-12-29 09:23:24

问题


I'am newbie in Android programming and I want to us jqMath to display some math formulas in WebView.

Here is my code:

    WebView webView = (WebView)findViewById(R.id.webView1);
WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
String js = "<html><head>"
    + "<link rel='stylesheet' href='jqmath-0.4.0.css'>"
    + "<script src='jquery-1.4.3.min.js'></script>"
    + "<script src='jqmath-etc-0.4.0.min.js'></script>"
    + "</head><body>"
    + "<script>var s = '$ax^2+bx+c=0$ with $a≠0$';M.parseMath(s);document.write(s);</script></body>";
webView.loadData(js,  "text/html",  "UTF-8");

What is the problem of this code?,

UPDATE well my problem have solved, but I also changed loadData function to loadDataWithBaseURL I mention just for reference if anyone else have same problem


回答1:


You need to specify the path correctly for the css,js files as following:

WebView webView = (WebView)findViewById(R.id.webView1);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
String path="file:///android_asset/";
String js = "<html><head>"
+ "<link rel='stylesheet' href='"+path+"jqmath-0.4.0.css'>"
+ "<script src='"+path+"jquery-1.4.3.min.js'></script>"
+ "<script src='"+path+"jqmath-etc-0.4.0.min.js'></script>"
+ "</head><body>"
+ "<script>var s = '$ax^2+bx+c=0$ with $a≠0$';M.parseMath(s);document.write(s);</script></body>";
webView.loadData(js,  "text/html",  "UTF-8");

note that the files should be in assets folder.




回答2:


the location of js lib in assets folder does not match your src declaration assuming that you placed the js libs in assets/js




回答3:


After specifying the path like Khalid said, the following worked for me

webView.loadDataWithBaseURL( "file:///android_asset/" ,js,  "text/html",  "UTF-8", null);

instead of webView.loadData method.



来源:https://stackoverflow.com/questions/22127355/using-jqmath-in-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!