Страница 1 из 1

WebView не обновляется

Добавлено: 26 ноя 2015, 12:40
j-test
нашел пример с MathJax. Там WebView обновляется по кнопке. мне нужно сделать чтобы обновлялось программно, сразу.
Не работает((

Код: Выделить всё

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;
	}
	
	private int exampleIndex = 3;
	
	private String getExample(int index) {
		return getResources().getStringArray(R.array.tex_examples)[index];
	}

	public void onClick(View v) {
		if (v == findViewById(R.id.button2)) {
			String example = "5^5";
			WebView w = (WebView) findViewById(R.id.webview);
			//EditText e = (EditText) findViewById(R.id.edit);
			w.loadUrl("javascript:document.getElementById('math').innerHTML='\\\\["
			           +"5^5"+"\\\\]';");
			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. */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_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","");

        String example = "5\\cdot5";
        //EditText e = (EditText) findViewById(R.id.edit);
        w.evaluateJavascript("javascript:document.getElementById('math').innerHTML='\\\\["
                + "6^6" + "\\\\]';", null);
        w.loadUrl("javascript:document.getElementById('math').innerHTML='\\\\["
                + "6^6" + "\\\\]';");
        w.evaluateJavascript("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);", null);

		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()));	
		
	}
}
собственно, в оригинале вот это делается в OnCreate:

Код: Выделить всё

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","");
а потом уже в обработке кликов загружается что нужно:

Код: Выделить всё

public void onClick(View v) {
		if (v == findViewById(R.id.button2)) {
			String example = "5^5";
			WebView w = (WebView) findViewById(R.id.webview);
			//EditText e = (EditText) findViewById(R.id.edit);
			w.loadUrl("javascript:document.getElementById('math').innerHTML='\\\\["
			           +example+"\\\\]';");
			w.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
		}
и это работает. но когда я пытаюсь это же сделать в oncreate то почему-то ничего не отображается...

Код: Выделить всё

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","");

        String example = "5\\cdot5";
        //EditText e = (EditText) findViewById(R.id.edit);
        w.evaluateJavascript("javascript:document.getElementById('math').innerHTML='\\\\["
                + "6^6" + "\\\\]';", null); //пробовал и через loadUrl и так и так, никак...
        w.loadUrl("javascript:document.getElementById('math').innerHTML='\\\\["
                + "6^6" + "\\\\]';");
        w.evaluateJavascript("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);", null);
почему через кнопки работает а так нет? может есть какие-то нюансы работы WebView? или это MathJax? или что-то еще...