【问题标题】:using javascript in android webview在android webview中使用javascript
【发布时间】:2012-05-15 09:58:07
【问题描述】:

我正在尝试从我的 web 视图中的 javascript 界面启动一项活动。 该示例显示吐司。我怎么能叫班级而不是敬酒呢?

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

这个用于 html 页面。

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

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

【问题讨论】:

  • 我没有足够的经验来回答,但你读过这个吗:developer.android.com/guide/webapps/…
  • 好吧,我只是没有看到你在 webview 中启用了 javascript 的任何地方,尽管它是相关的。
  • 你没有得到我的问题,我想调用一个意图而不是敬酒。我已经使敬酒的示例起作用了。还是谢谢
  • 我想在没有任何 webview 的情况下这样做。只是在内部是可能的。

标签: javascript android webview


【解决方案1】:

你必须先在你的 webview 上注册 JavaScriptInterface。 JavaScriptInterFace 可以是一个内部类,如下所示。这个类将有一个函数,你可以从 html 页面调用(通过 javaScript),在这个函数中你可以编写代码来改变活动。

这是适合您的工作解决方案:

public class JavascriptInterfaceActivity extends Activity {
    /** Called when the activity is first created. */


    WebView wv;

    JavaScriptInterface JSInterface;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        wv = (WebView)findViewById(R.id.webView1);

        wv.getSettings().setJavaScriptEnabled(true);
        // register class containing methods to be exposed to JavaScript

        JSInterface = new JavaScriptInterface(this);
        wv.addJavascriptInterface(JSInterface, "JSInterface"); 

        wv.loadUrl("file:///android_asset/myPage.html");

    }


    public class JavaScriptInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        JavaScriptInterface(Context c) {
            mContext = c;
        }

        @android.webkit.JavascriptInterface
        public void changeActivity()
        {
            Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class);
            startActivity(i);
            finish();
        }
    }
}

这里是html页面

<html>
<head>
<script type="text/javascript">
function displaymessage()
{
JSInterface.changeActivity();
}
</script>
</head>

<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>

希望这会有所帮助...

【讨论】:

  • 谢谢我的朋友。它几乎帮助我解决了我的问题。
  • @Common 我应该把上面的 html 代码放在单独的 html 文件中?
  • 这对于从 javascript 调用 android 函数很有用,但是有什么方法可以从 android 代码调用 javascript 函数?
  • 这个 html 文件的位置是什么?
  • 我有一个问题,把 HTML 页面放在哪里?
【解决方案2】:

如果您在 Android 4.2 或更高版本上运行,您还需要在您的 Android 代码中的 changeActivity 方法顶部添加 @android.webkit.JavascriptInterface 注释。 请参阅此link 了解更多信息。

【讨论】:

    猜你喜欢
    • 2018-09-09
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2013-06-06
    • 2011-05-18
    相关资源
    最近更新 更多