0
爲什麼我的代碼不會觸發我的JavaScriptInterface:當webview加載時,我看不到任何對話框彈出窗口(我猜android.widget.Toast應該這樣做)。JavaScriptInterface不叫
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MyActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String data = "<html>" +
"<body><h1>Test JavaScriptInterface</h1></body>" +
"<script>Android.showToast(toast);</script>" +
"</html>";
WebView engine = (WebView) findViewById(R.id.web_engine);
engine.getSettings().setJavaScriptEnabled(true);
engine.addJavascriptInterface(new JavaScriptInterface(this), "Android");
engine.loadData(data, "text/html", "UTF-8");
}
}
package com.example;
import android.content.Context;
import android.widget.Toast;
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
是由於我與語法這麼笨:) – user310291 2012-02-06 20:43:48