我一直在从事对数学应用程序进行排版的移动应用程序的开发,但是我遇到了一个问题。按照规则,代码可以,但表达式不会更改。没有排版。
这是我的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);
}
}
最佳答案
对不起,自我提升。但是也许您可以尝试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>
希望这可以帮助!