简体   繁体   English

Clickevents 在 web 视图 android 中不起作用

[英]Clickevents are not working in web view android

I have to create we application in android.我必须在android中创建我们的应用程序。

So what i done is that,simply created raw folder under res and put html files there.所以我所做的是,简单地在res下创建原始文件夹并将 html 文件放在那里。

All works fine but when i click a button that is put inside that web page nothing happens and the click event not get work.一切正常,但是当我单击放置在该网页中的按钮时,没有任何反应,并且单击事件不起作用。

Here are my codes.这是我的代码。

newfile.html新文件.html

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form>
<input type="button" value="Click me" onclick="openDialog()">
<script>
function openDialog()
{

alert("ok");
}
</script>
</form>
</body>
</html>

and this is my java code,这是我的java代码,

webview.loadData(readTextFromResource(R.raw.newfile),"text/html", "utf-8");

readTextFromResource function readTextFromResource 函数

    private String readTextFromResource(int resourceID)

{
    InputStream raw = getResources().openRawResource(resourceID);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    int i;
    try
    {
        i = raw.read();
        while (i != -1)
        {
           stream.write(i);
            i = raw.read();
        }
        raw.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
return stream.toString();
}

Please some one point me why the click event not working !请告诉我为什么点击事件不起作用!

I am doing this type of things using phonegap.我正在使用 phonegap 做这种类型的事情。 If you want to call native functions, use java script enabled web view.如果要调用本机函数,请使用启用了 Java 脚本的 Web 视图。 I am extends DroidGap in my MainActivity and my Login.html file under assets folder我在 MainActivity 和资产文件夹下的 Login.html 文件中扩展了 DroidGap
in your Main Activity.java,在您的主 Activity.java 中,

WebView webView=new WebView(this);
webview.loadUrl("file:///android_asset/www/Phone/Login.html");
setContentView(webView);

Anyway, if problem not solved, try adding this (all codes in onCreate method)无论如何,如果问题没有解决,请尝试添加这个(onCreate方法中的所有代码)

    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setSaveFormData(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAllowFileAccessFromFileURLs(true);
    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webView.getSettings().setSupportZoom(true);
    webView.setWebViewClient(new WebViewClient());
    webView.setClickable(true);
    webView.setWebChromeClient(new WebChromeClient());

If you want more about how to access Android native code through webpage here is the link如果您想了解更多有关如何通过网页访问 Android 本机代码的信息,请访问这里的链接

When a WebView doesn't respond to a button, you can test a web page inside a mobile browser or a desktop browser with minimum width (adaptive layout).WebView不响应按钮时,您可以在移动浏览器或桌面浏览器中以最小宽度(自适应布局)测试网页。 You will see how the button works when you click it.当您单击它时,您将看到该按钮是如何工作的。 Then you can press F12 and see what queries are sent in "Network" tab.然后您可以按F12并查看在“网络”选项卡中发送的查询。

在此处输入图片说明

(If you press Ctrl + Shift + C , you will see a layout of the page, then can click an element (the button that you look for) and see a code.) (如果您按Ctrl + Shift + C ,您将看到页面的布局,然后可以单击一个元素(您要查找的按钮)并查看代码。)

If the button shows a picture dialog (take a photo, upload a picture), you should use onShowFileChooser ( [1] ).如果按钮显示图片对话框(拍照、上传图片),则应使用onShowFileChooser ( [1] )。

val startActivityForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
    //
}

@SuppressLint("SetJavaScriptEnabled")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    with(webview) {
        settings.javaScriptEnabled = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.domStorageEnabled = true
        // webViewClient = MyWebViewClient()
        webChromeClient = MyWebChromeClient()
    }
}

private class MyWebChromeClient : WebChromeClient() {

    override fun onShowFileChooser(
        webView: WebView?,
        filePathCallback: ValueCallback<Array<Uri>>?,
        fileChooserParams: FileChooserParams?
    ): Boolean {
        // Check permissions, create intent.
        startActivityForResult.launch(chooserIntent)
        return true
    }
}

In case you have a JavaScript button, you can attach JavaScript interfac e ( [1] , [2] , [3] ):如果你有一个 JavaScript 按钮,你可以附加JavaScript 接口[1][2][3] ):

@SuppressLint("SetJavaScriptEnabled")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    with(webview) {
        settings.javaScriptEnabled = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.domStorageEnabled = true
        // WebView.setWebContentsDebuggingEnabled(BuildConfig.TEST)
        addJavascriptInterface(
            JsHandle(this@WebViewFragment),
            "Android"
        )
    }
}

class JsHandle(private var fragment: WebViewFragment) {

    @JavascriptInterface
    fun notify(notify: String) {
        //
    }
}

Your web page should have javascript: inside.你的网页应该有javascript: inside。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM