WebView--简单调用javascripter

public class MainActivity extends Activity {

	private WebView web;
	private WebSettings settings;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		web = (WebView) findViewById(R.id.w_wb);
		initSetting();
		web.loadUrl("http://192.168.56.1:8080/jstest.html");
		/*
		 * 如何去和js交互一方工程师设定接口
		 * 添加js接口
		 * name接口的名称
		 * 网页中java脚本javascript:window.javaCode.setinfo(uname,uclass);
		 */
		web.addJavascriptInterface(new Object(){
			//对于4.0以后版本,添加注解
			@android.webkit.JavascriptInterface
			public void setinfo(String name,String mclass){
				Toast.makeText(MainActivity.this, "名称"+name+"班级 "+mclass, 0).show();
			}
		}, "javaCode");
	}

	private void initSetting() {
		settings = web.getSettings();
		//设置支持js
		settings.setJavaScriptEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	public void useJsMethod(View view){
		//_onsubmit()
		web.loadUrl("javascript:_onsubmit()");
	}
}

你可能感兴趣的:(WebView--简单调用javascripter)