1

我一直在開發排版數學應用程序的移動應用程序,但我面臨一個問題。該代碼是按照規則確定的,但表達式不會更改。不排版正在發生。開發一個離線MathJax Android移動應用程序

這是我的MainActivity代碼,請你告訴我哪裏還有漏洞:

public class MainActivity extends Activity implements View.OnClickListener 
{ 

    private String doubleEscapeTeX(String s) { 
     String t=""; 
     for (int i=0; i < s.length(); i++) { 
     if (s.charAt(i) == '\'') t += '\\'; 
     if (s.charAt(i) != '\n') t += s.charAt(i); 
     if (s.charAt(i) == '\\') t += "\\"; 
    } 
    return t; 
} 

public void onClick(View v) { 
    if (v == findViewById(R.id.button2)) { 
     WebView w = (WebView) findViewById(R.id.webview); 
     EditText e = (EditText) findViewById(R.id.edit); 
     w.loadUrl("javascript:document.getElementById('math').innerHTML='\\\\[" 
        +doubleEscapeTeX(e.getText().toString())+"\\\\]';"); 
     w.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);"); 
    } 
    else if (v == findViewById(R.id.button3)) { 
     WebView w = (WebView) findViewById(R.id.webview); 
     EditText e = (EditText) findViewById(R.id.edit); 
     e.setText(""); 
     w.loadUrl("javascript:document.getElementById('math').innerHTML='';"); 
     w.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);"); 
    } 
} 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    WebView w = (WebView) findViewById(R.id.webview); 
    w.getSettings().setJavaScriptEnabled(true); 
    w.getSettings().setBuiltInZoomControls(true); 
    w.loadDataWithBaseURL("http://bar", "<script type='text/x-mathjax-config'>" 
          +"MathJax.Hub.Config({ " 
          +"showMathMenu: false, " 
          +"jax: ['input/TeX','output/HTML-CSS'], " 
          +"extensions: ['tex2jax.js'], " 
          +"TeX: { extensions: ['AMSmath.js','AMSsymbols.js'," 
           +"'noErrors.js','noUndefined.js'] } " 
          +"});</script>" 
          +"<script type='text/javascript' " 
          +"src='file:///android_asset/MathJax/MathJax.js'" 
          +"></script><span id='math'></span>","text/html","utf-8",""); 
    EditText e = (EditText) findViewById(R.id.edit); 
    e.setBackgroundColor(Color.LTGRAY); 
    e.setTextColor(Color.BLACK); 
    e.setText(""); 
    Button b = (Button) findViewById(R.id.button2); 
    b.setOnClickListener(this); 
    b = (Button) findViewById(R.id.button3); 
    b.setOnClickListener(this); 

} 
} 
+0

也許這可以幫助:HTTP://stackoverflow.com/q/17029780/1919013 – Neoh 2015-03-22 14:47:32

回答

5

對不起,自我宣傳。但也許你可以試試MathView。這是一個基於Android WebView的自定義視圖庫,並使用MathJax來顯示數學公式。

MathView的用法與TextView一樣,但它會自動呈現TeX代碼。

<io.github.kexanie.library.MathView 
    android:id="@+id/formula_one" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    auto:text="When \\(a \\ne 0\\), there are two solutions to \\(ax^2 + bx + c = 0\\) and they are $$x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.$$" 
    > 
</io.github.kexanie.library.MathView> 

希望這有助於!

0

使用此代碼

w.loadDataWithBaseURL("http://bar", 
"<html><head>" + 
" <meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" />" + 
"</head>" + 
"" + 
"<body style=\"font-size:18px\" >" + 
"<b>Put here your equation</b> </br> " + 
"<script type=\"text/x-mathjax-config\">" + 
" MathJax.Hub.Config({\n" + 
" CommonHTML: { linebreaks: { automatic: true },EqnChunk:(MathJax.Hub.Browser.isMobile?10:50) },displayAlign: \"left\",\n" + 
" \"HTML-CSS\": { linebreaks: { automatic: true } ," + 
"\n" + 
" preferredFont: \"STIX\"}," + 
"extensions: [\"tex2jax.js\"],messageStyle:\"none\"," + 
"jax: [\"input/TeX\", \"input/MathML\",\"output/HTML-CSS\"]," + 
"tex2jax: {inlineMath: [['$','$'],['\\\\(','\\\\)']]}" + 
"});" + 
"</script>" + 
"<script type=\"text/javascript\" async src=\"file:///android_asset/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script>" + 
"" + 
"</body>" + 
"</html>", "text/html", "utf-8", ""); 
+0

您可以加入你改變/製作,爲什麼什麼解釋? – Dekker 2017-07-27 13:24:22

+0

@Dekker我好吧,我對HTML沒有太多的瞭解,但是我花了很多時間尋找解決方案,而且這段代碼與最新的** MathJax **離線庫一起工作良好。 – 2017-07-29 07:10:39