public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText ed;
private Button btn;
private ProgressBar progress;
private WebView webview;
private Button js;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
ed = (EditText) findViewById(R.id.ed);
btn = (Button) findViewById(R.id.btn);
progress = (ProgressBar) findViewById(R.id.progress);
webview = (WebView) findViewById(R.id.webview);
js = (Button) findViewById(R.id.js);
btn.setOnClickListener(this);
js.setOnClickListener(this);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
// Android端定义一个方法,给js调用,
// 使用webView对象,调用addJavascriptInterface方法(),
// 第一个参数是写一个类,在这里面提供要暴露的方法,方法前最好加一个注解:@JavascriptInterface,
// 第二个参数是标识字符串,js通过这个标识,调用我们的方法. 在js里面是这样使用的:Android.showToast(content);
webview.addJavascriptInterface(new Object() {
@JavascriptInterface
public void showToast(String content) {
Toast.makeText(getApplicationContext(), "哇塞!!!!", Toast.LENGTH_LONG).show();
}
}, "Android");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
//加载js页面
webview.loadUrl("http://172.17.8.100/images/small/default/test.html\n");
break;
case R.id.js:
//调用html中的js方法 格式固定:webView对象.loadUrl("javascript:js方法名(参数)");
webview.loadUrl("javascript:changeInputValue('哈哈哈哈哈哈')");
break;
}
}
}
Webview与js交互
猜你喜欢
转载自blog.csdn.net/KWON_QMY/article/details/84347944
今日推荐
周排行