gpt4 book ai didi

java - 在安卓中使用 Mathjax

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:43 24 4
gpt4 key购买 nike

您好,我想在我的 Android 应用程序中使用 Mathjax,为此我找到了一个示例表单 Here .
我想在 Activity 启动时显示一个公式,而不是像您按下显示按钮时的示例,但这不起作用。我的代码中是否遗漏了某些内容?
感谢您的帮助。

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;
}

private int exampleIndex = 0;

private String getExample(int index) {
return getResources().getStringArray(R.array.tex_examples)[index];
}

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]);");
}
else if (v == findViewById(R.id.button4)) {
WebView w = (WebView) findViewById(R.id.webview);
EditText e = (EditText) findViewById(R.id.edit);
e.setText(getExample(exampleIndex++));
if (exampleIndex > getResources().getStringArray(R.array.tex_examples).length-1)
exampleIndex=0;
w.loadUrl("javascript:document.getElementById('math').innerHTML='\\\\["
+doubleEscapeTeX(e.getText().toString())
+"\\\\]';");
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);
b = (Button) findViewById(R.id.button4);
b.setOnClickListener(this);
TextView t = (TextView) findViewById(R.id.textview3);
t.setMovementMethod(LinkMovementMethod.getInstance());
t.setText(Html.fromHtml(t.getText().toString()));

// Here is my added code.
w.loadUrl("javascript:document.getElementById('math').innerHTML='\\\\["
+doubleEscapeTeX("\\frac{1}2")+"\\\\]';");
w.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");

}

最佳答案

您必须将 Assets 文件夹复制到应用程序的 Assets 文件夹中。

关于java - 在安卓中使用 Mathjax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com