我是android开发的新手。我正在尝试开发一个用于查看数学公式的应用程序。我的应用程序在WebView中显示了该公式,但在加载消息时使用了javascript。我想修剪从WebView加载消息的数据。

我的xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EFEFEF"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp" >

<LinearLayout
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#ffd0e3e5"
    android:gravity="center"
    android:paddingBottom="6dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#ff33203d"
        android:textSize="18sp" />
</LinearLayout>

<WebView
    android:id="@+id/txtQuestion"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_below="@+id/topLayout"
    android:layout_weight="1" >
   </WebView>
 </RelativeLayout>

我的Java代码:
  public class MainActivity extends Activity implements View.OnClickListener{

 private WebView txtQuestion;
 private String mathML;

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

 @Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.examination_fragment_layout);
txtQuestion = (WebView) findViewById(R.id.txtQuestion);
txtQuestion.getSettings().setJavaScriptEnabled(true);

try {
    InputStream raw = getApplicationContext().getAssets().open(
            "multipale_choose.xml");
    QuizzesParser parser = new QuizzesParser(this);
    parser.startParsing(raw);
    answerList=parser.getpList();
   // initiateWebView(answerList.get(1).getAns_body(),txtQuestion);

    mathML="<p style='  height: 1000px;vertical-align: middle;text-align:center;'>";
mathML=mathML+answerList.get(1).getAns_body();

mathML=mathML+"</p>";

txtQuestion=formulaWebview;
txtQuestion.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

             txtQuestion.evaluateJavascript("javascript:document.getElementById('math').innerHTML='" +
                 doubleEscapeTeX(mathML)
                              +"';",null);


        }
        else{
            txtQuestion.loadUrl("javascript:document.getElementById('math').innerHTML='" +
                    doubleEscapeTeX(mathML)+"';");

        }

        txtQuestion.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
    }
});

final String mathJaxOfflineUrl = "file:///android_asset/MathJax/MathJax.js";
txtQuestion.loadDataWithBaseURL("http://bar/", "<script type='text/x-mathjax-config'>"
        +"MathJax.Hub.Config({ "
            +"showMathMenu: false, "
            +"jax: ['input/MathML','output/HTML-CSS'], "

                     +"extensions: ['mml2jax.js'], "
                      +"TeX: { extensions: ['noErrors.js','noUndefined.js'] }, "

          +"});</script>"
        +"<script type='text/javascript' "
          +"src='"+mathJaxOfflineUrl+"'"
          +"></script><span id='math'></span>","text/html","utf-8","");

  }catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
 }
}

我正在关注example

最佳答案

您将此代码添加到您的Webviews“loadDataWithBaseURL”

txtQuestion.loadDataWithBaseURL("http://bar/", "<script type='text/x-mathjax-config'>"
    +"MathJax.Hub.Config({ "
    +"showMathMenu: false, "
    +"jax: ['input/MathML','output/HTML-CSS'], "
    +"extensions: ['mml2jax.js'], "
    +"TeX: { extensions: ['noErrors.js','noUndefined.js'] }, "
    +"messageStyle: 'none'"
    +"});</script>"
    +"<script type='text/javascript' "
    +"src='"+mathJaxOfflineUrl+"'"
    +">
   </script><span id='math'></span>","text/html","utf-8","");

08-17 05:59