Android:即使使用setJavaScriptEnabled(true)也无法让javascript在WebView上运行

Hot*_*day 5 javascript android webview

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

这是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");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是来自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>
Run Code Online (Sandbox Code Playgroud)

另外,这是我在Manifest上改变的全部内容:

<activity android:name=".HelloWebApp"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
android:screenOrientation="landscape">
Run Code Online (Sandbox Code Playgroud)

至于javascript,我已经尝试了一切.复杂,简单,在底部的身体内,在按钮上,在头上.什么都行不通.HTML工作正常.

在此先感谢您的帮助.

no.*_*ing 21

你错过了他补充的教程中的部分

webView.setWebChromeClient(new WebChromeClient());
Run Code Online (Sandbox Code Playgroud)

在添加之后

webView.getSettings().setJavaScriptEnabled(true);
Run Code Online (Sandbox Code Playgroud)

这种方法JavaDoc说:

设置chrome处理程序.这是WebChromeClient的一个实现,用于处理Javascript对话框,favicon,标题和进度.这将取代当前的处理程序.

  • 感谢 no.good.at.coding。虽然那应该是我的名字。;-) (3认同)