加载Android时如何调用javascript方法
我是android的初学者 我在android中使用了javascript方法。我想在第一次加载应用程序时调用它 比如说,加载Android时如何调用javascript方法,javascript,android,hybrid-mobile-app,Javascript,Android,Hybrid Mobile App,我是android的初学者 我在android中使用了javascript方法。我想在第一次加载应用程序时调用它 比如说, testMethod = function(str) { alert("hi" + str); }//javascript 及 我在这里想的是,当我启动应用程序时,警报会立即弹出 但什么也没发生 关于这一点,我有两个问题 1.我可以在Android的onCreate方法中调用并使用javascript方法吗 2.如果可能,我该怎么办?如果您想在启动时显示弹出窗口,只需
testMethod = function(str) {
alert("hi" + str);
}//javascript
及
我在这里想的是,当我启动应用程序时,警报会立即弹出
但什么也没发生
关于这一点,我有两个问题
1.我可以在Android的onCreate方法中调用并使用javascript方法吗
2.如果可能,我该怎么办?如果您想在启动时显示弹出窗口,只需使用默认的AlertDialog。请参阅下面的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showMessage("My Message Here", "Title here");
}
private void showMessage(String data, String title){
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(data)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue
}
})
.show();
}
mWebView.loadUrl(“javascript:testMethod('javascript')”)
放在view.post或view.postDelayed中谢谢你的回答!这很好,但我真正想做的是获取javascript的值并将其打印出来。最后,我将在android中使用该值。我可以用你的答案来做这件事吗?html页面加载得很好,但仍然不起作用。我认为,这可能是javascript错误。谢谢你的帮助!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showMessage("My Message Here", "Title here");
}
private void showMessage(String data, String title){
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(data)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue
}
})
.show();
}