gpt4 book ai didi

javascript - Android:即使使用 setJavaScriptEnabled(true) 也无法让 javascript 在 WebView 上工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:50:35 25 4
gpt4 key购买 nike

我是 Android 的完全菜鸟,这只是一个简单的测试。 Based it on this tutorial.

这里是 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 的:

<?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>

此外,这就是我在 list 上所做的全部更改:

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

至于javascript,我什么都试过了。复杂的,简单的,在底部的 body 里面,在一个按钮里,在头上。什么都不管用。 html 工作正常。

在此先感谢您的帮助。

最佳答案

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

webView.setWebChromeClient(new WebChromeClient());

添加后立即

webView.getSettings().setJavaScriptEnabled(true);

JavaDoc for this method说:

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.

关于javascript - Android:即使使用 setJavaScriptEnabled(true) 也无法让 javascript 在 WebView 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5110916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com