这里是我的代码,这是字面上步骤从developer.android.com敬酒不工作的WebView
一步,它只是不工作,不管有多少次我运行它。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.loadUrl("http://www.google.com");
}
@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 class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
}
当我运行我的应用程序,网页会加载,敬酒从不显露。
我似乎无法找到问题。有人能告诉我这是否适合他们吗?
编辑:这里有一些我很困惑。在此之后,指令是
这创建了一个称为Android for JavaScript的界面,运行在 WebView中。此时,您的Web应用程序可以访问 WebAppInterface类。例如,这里的一些HTML和JavaScript 创建使用新界面敬酒消息,当用户 点击一个按钮:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
我不知道在哪里的js代码的那部分去要么..
实际上调用'showToast()'的代码在哪里? – laalto
我认为你误读了本教程。它说:_“在这个例子中,WebAppInterface类允许网页使用showToast()方法创建Toast消息。”_尝试创建一个html页面,并将其加载到webview中,就像它在教程。 –
它是在谷歌网站的代码? – user1940676