How to load an external url in Codename One WebBrowser component?

浪尽此生 提交于 2019-12-11 09:43:33

问题


Can anyone help me? I've got an App in development and a core feature is to load a third party webpage (URL) that has css and javascript in the Codename One WebBrowser component. My questions are:- 1. How do I load the url? 2. Is it possible to interact with the javascript on this url like alerts and confirms? 3. How to switch between Device default browser etc?

Thanks.


回答1:


A native WebBrowser component is laid out a-synchronously which means its preferred size is calculated later and so the layout places it with a 0,0 size. On RIM we default to using the legacy HTMLComponent (from J2ME) since RIM's native browser component is flaky, that is why the RIM version works for you while the Android version does not.

The HTMLComponent supports only a subset of HTML/CSS and doesn't support any JavaScript so this is probably not what you would want to use. You can enable the native RIM browser component (although it is flaky and has many known issues with no workaround) by setting the build argument: rim.nativeBrowser=true

You can set these arguments in the project properties for Codename One, if it works for you then great.

To fix the layout issue you can use a layout like BorderLayout when placing the browser e.g.:

form.setLayout(new BorderLayout());
form.addComponent(BorderLayout.CENTER, browser);

This will stretch the component over the screen ignoring its preferred size. Alternatively you can track browser events and revalidate or hardcode a preferred size value (which I wouldn't recommend).



来源:https://stackoverflow.com/questions/16618837/how-to-load-an-external-url-in-codename-one-webbrowser-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!