Android:即使使用 setJavaScriptEnabled(true) 也无法让 javascript 在 WebView 上工作 [英] Android: can't get javascript to work on WebView even with setJavaScriptEnabled(true)

查看:36
本文介绍了Android:即使使用 setJavaScriptEnabled(true) 也无法让 javascript 在 WebView 上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 的完全菜鸟,这只是一个简单的测试.基于本教程.

I'm a complete noob to Android and this is just a simple test. Based it on this tutorial.

这里是 HelloWebApp.java

Here goes the HelloWebApp.java

package com.company.something;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class HelloWebApp extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("file:///android_asset/www/index.html");
    }
}

这是来自 res/layout/main.xml:

And this is from res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<WebView  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/webView"
    />
</LinearLayout>

此外,这就是我在 Manifest 上更改的全部内容:

Plus this is all I changed on the Manifest:

<activity android:name=".HelloWebApp"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
android:screenOrientation="landscape">

至于 javascript,我什么都试过了.复杂的,简单的,在身体底部,在一个按钮里,在头上.没有任何作用.html 工作正常.

As for the javascript, I've tried everything. Complicated, simple, inside the body at the bottom, in a button, on the head. Nothing works. The html works fine.

在此先感谢您的帮助.

推荐答案

您错过了教程中他添加的部分

You missed the part in the tutorial where he adds

webView.setWebChromeClient(new WebChromeClient());

添加后立即

webView.getSettings().setJavaScriptEnabled(true);

此方法的 JavaDoc 说:

设置镀铬处理程序.这是 WebChromeClient 的一个实现,用于处理 Javascript 对话框、网站图标、标题和进度.这将替换当前的处理程序.

Sets the chrome handler. This is an implementation of WebChromeClient for use in handling Javascript dialogs, favicons, titles, and the progress. This will replace the current handler.

这篇关于Android:即使使用 setJavaScriptEnabled(true) 也无法让 javascript 在 WebView 上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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