Toast在webview中不起作用 [英] Toast does not work in webview

查看:166
本文介绍了Toast在webview中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,这是逐步从developer.android.com

Here is my code, this is literally step by step from developer.android.com

,无论我运行了多少次,它都无法正常工作它。

and it just doesn't work, no matter how many times I run it.

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();
        }
    }

}

当我运行我的应用程序,网页将加载,烤面包从不显示。

When I run my app, the webpage will load, toast never shows.

我似乎无法找到问题。有人可以告诉我这是否对他们有效吗?

I just can't seem to find the problem. Can someone tell me if this works for them?

编辑:这是我对此感到困惑的一些问题。在此之后,说明是

Here is some more that I'm confused about. Right after this, the instructions are


这将在
WebView中创建一个名为Android for JavaScript的界面。此时,您的Web应用程序可以访问
WebAppInterface类。例如,这里有一些HTML和JavaScript
,当用户
点击按钮时,使用新界面创建一个Toast消息:

This creates an interface called Android for JavaScript running in the WebView. At this point, your web application has access to the WebAppInterface class. For example, here's some HTML and JavaScript that creates a toast message using the new interface when the user clicks a button:



<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

我不知道js代码的哪一部分去了..

I have no idea where that part of js code goes either..

推荐答案


我不知道js代码的哪一部分去了..

I have no idea where that part of js code goes either..

在您的资产文件夹中创建一个html页面,假设名为 myWonderfulWebPage.html

Create an html page in your assets folder, let's say named myWonderfulWebPage.html.

将以下HTML代码复制到其中:

Copy the following HTML code to it :

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

如您所见,当您点击按钮时,功能将调用showAndroidToast ,此函数将调用您在Java代码中定义的函数。

As you can see, when you'll click the button, the function showAndroidToast will be called and this function will call the one you defined in your Java code.

现在返回您的活动并加载此页面进入你的网页浏览:

Now go back in your activity and load this page into your webview :

myWebView.loadUrl("file:///android_asset/myWonderfulWebPage.html");

现在您将看到它显示一个空白页面,带有一个按钮。点击它,它会在你的网页上显示 Toast

Now you will see that it shows a blank page, with a button. Click it and it will show you the Toast on your webpage.

这篇关于Toast在webview中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆