Toast does not work in webview

北慕城南 提交于 2019-12-22 19:26:51

问题


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?

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

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>

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


回答1:


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

Create an html page in your assets folder, let's say named myWonderfulWebPage.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>

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");

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.




回答2:


If the code you show is what is actually used then of course it will not work.

myWebView.loadUrl("http://www.google.com");

Google.com does not invoke the JS function you have added a bridge to. You need to use your own webpage that does indeed invoke the function, either local or on the web.-.-



来源:https://stackoverflow.com/questions/21215194/toast-does-not-work-in-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!