0

からメソッドを呼び出していWebViewます。このメソッドは、Toastのボタンがクリックされるたびに を実行しWebViewます。正常に動作しますが、引数を渡すと動作しません。引数を渡すと、メソッドが実行されません。

これは私のものjavascriptですstrings.xml-

<string name="details">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width; user-scalable=0;&quot; /&gt;
&lt;title&gt;My HTML&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;MyHTML&lt;/h1&gt;
&lt;p id=&quot;mytext&quot;&gt;Hello!&lt;/p&gt;
&lt;input type=&quot;button&quot; value=&quot;Say hello&quot; onClick=&quot;showAndroidToast('Hello world!')&quot; /&gt;
&lt;script language=&quot;javascript&quot;&gt;
   function showAndroidToast(toast) {
       AndroidFunction.showToast(toast);
   }
   function callFromActivity(msg){
 document.getElementById(&quot;mytext&quot;).innerHTML = msg;
   }
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;</string>

これは私のActivity'sコードです-

String str = "<html><body>"
            + getString(R.string.details)
            + "</body></html>";
    webview.addJavascriptInterface(new MyJavaScriptInterface(this),
            "AndroidFunction");

    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.loadDataWithBaseURL(null, str, "text/html", "utf-8", null);

これは私のMyJavaScriptInterfaceクラスです:

public class MyJavaScriptInterface {
    Context mContext;

    MyJavaScriptInterface(Context c) {
        Log.d("tag", "cls");
        mContext = c;
    }

    public void showToast(String toast) {//without the argument this method executes
        Log.d("tag", "msg");
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }

}
4

2 に答える 2