简体   繁体   English

如何在 webview 中获取我的图像的“src”属性并通过意图传递它?

[英]How to get 'src' attribute of my image in webview and pass it via intent?

I'm showing some images from my assets folder using webview and i want to define an onclick listener for my specific image so when user clicks on it, it shows in a new intent in full scale.我正在使用 webview 显示我的资产文件夹中的一些图像,并且我想为我的特定图像定义一个 onclick 侦听器,因此当用户单击它时,它会以全新的意图全面显示。

    private void showDescription() {
    final WebView webView = findViewById(R.id.webView);
    webView.loadUrl("file:///android_asset/" + main_menu_title + "/" + sub_menu_title + "/index.html" );
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setSupportZoom(false);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new Object()
    {
        @JavascriptInterface
        public void performClick()
        {

        }
    }, "ok");

}

this is my image and onclick function for it:这是我的图像和 onclick function :

<img src="img_desc00.png" alt="Description" style="width:500px; height:500px;

margin-left:auto;左边距:自动; margin-right:auto;边距右:自动; display:block;显示:块; " onclick="ok.performClick();"> " onclick="ok.performClick();">

I want to get src attribute of the image so i can pass it via intent to show in another activity我想获取图像的 src 属性,以便我可以通过意图传递它以在另一个活动中显示

In the onclick attribute you can access to the current html element with the keyword this and to his attributes with him getAttribute method so you can do:在 onclick 属性中,您可以使用关键字 this 访问当前 html 元素,并使用他的 getAttribute 方法访问他的属性,因此您可以执行以下操作:

<img src="..." onclick="ok.performClick(this.getAttribute('src'))">

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

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