如何在Android Webview中按ID捕获元素点击? [英] How to catch element click by ID in Android webview?

查看:387
本文介绍了如何在Android Webview中按ID捕获元素点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有viewPager组件,其中包含带有远程服务器HTML内容的多个Web视图.

I have the viewPager component which is containing the several webviews with HTML content from remote server.

它是简单的HTML代码,无法更改服务器端的HTM1输出.

Is it simple HTML code without possibility to change the HTMl output on the server side.

我想问一下,如何在Android中捕获具有给定ID的指定元素上的click(tap)事件?

I would like to ask, how can i catch the click(tap) event on the specified element with the given ID in Android?

ViewPager

private void initViewPager() {
        pager = (ViewPager) findViewById(R.id.my_pager);
        adapter = new FragmentStatePagerAdapter(
                getSupportFragmentManager()
        ) {
            @Override
            public int getCount() {
                // This makes sure getItem doesn't use a position
                // that is out of bounds of our array of URLs
                Logger.d(String.valueOf(mWelcomeController.loadedPagesToDisplay.size()));
                return mWelcomeController.loadedPagesToDisplay.size();
            }

            @Override
            public android.support.v4.app.Fragment getItem(int position) {
                Logger.d(mWelcomeController.loadedPagesToDisplay.toString());
                return BrowserFragment.newInstance(
                        mWelcomeController.loadedPagesToDisplay.get(position)
                );
            }
        };

        //Let the pager know which adapter it is supposed to use
        pager.setAdapter(adapter);
    }

因为我无法在服务器端修改HTML输出(也许将某些属性注入设备上的DOM?),所以我不能使用类似的东西:

Because I cannot modify the HTML output on the server side (maybe inject some attributes into DOM on device ?) I cannot use something like that:

http ://www.scriptscoop.com/t/21b53b896c9e/javascript-how-to-to-detect-button-click-in-webview-android.html

在Android中通过javascript检测HTML按钮WebView .

我只想要这样的东西:

  • 在HTML代码中找到给定的元素
  • 更新HTML代码(添加 onclick事件)
  • 使用本机代码捕获此事件
  • Find the given element in the HTML code
  • Update the HTML code (add onclick event)
  • Catch this event in native code

推荐答案

为此,您需要解析html,对于Java(因此也是Android)来说,好的html解析器是

For that you need to parse the html, a good html parser for Java (and therefor also Android) is Jsoup.

您可以执行以下操作:

// Connect to the web site
Document doc = Jsoup.connect(url).get();
Element button = doc.select("#buttonid");
button.html("new stuff here");
//parse back and put in webview
String finaloutput = doc.html();

这篇关于如何在Android Webview中按ID捕获元素点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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