0
我可以將數據從我的android傳遞給javascript。但問題是,當我調用函數setValue()時,它變成了Undefined,但是當它加載時它傳遞了正確的值。以下是我的代碼。調用javascript函數時未定義
這裏是我的Java類代碼
public class PaymentActivity extends Activity {
String amount1;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.payment);
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
amount1 = extras.getString("amount1");
}
WebView web = (WebView) findViewById(R.id.web1);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("javascript:window.onload = function(){setValue(\""+ amount1 +"\");};");
web.loadUrl("file:///android_asset/www/index.html");
}
}
,這裏是我的網頁代碼
<!DOCTYPE html>
<html>
<head>
</head>
<body>
Whats your Name?
<input id="name" value="" />
<input id="pass" value="" />
<button onclick = "setValue()">Submit</button>
<script type="text/javascript">
function setValue(amount1){
myValue = amount1;
document.getElementById("pass").value = myValue;
}
</script>
</body>
</html>