问题
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