public class WebView extends Component
This class is inherited from Component and contains all capabilities of the basic component.
This component can be used to load web page content, obtain page details, and control page jumping. You can also use
Data abilities to load local web page files.
Component.BindStateChangedListener, Component.CanAcceptScrollListener, Component.ClickedListener, Component.ComponentStateChangedListener, Component.ContourRefreshedListener, Component.DefSpeechEventListener, Component.DoubleClickedListener, Component.DragFeedbackProvider, Component.DraggedListener, Component.DrawTask, Component.EditEventListener, Component.EstimateSizeListener, Component.EstimateSpec, Component.FadeEffectEnum, Component.FocusChangedListener, Component.ForwardHoverListener, Component.ForwardTouchListener, Component.GestureType, Component.HoverEventListener, Component.KeyEventListener, Component.LayoutDirection, Component.LayoutRefreshedListener, Component.LongClickedListener, Component.MeasureSpec, Component.MouseEventListener, Component.MousePointerStyleListener, Component.OnDragListener, Component.RotationEventListener, Component.ScaledListener, Component.ScrolledListener, Component.ShadowDevice, Component.ShadowMode, Component.ShadowSize, Component.SpeechEventListener, Component.TouchEventListener, Component.UnconsumedKeyEventListener, Component.VoiceEventACCESSIBILITY_ADAPTABLE, ACCESSIBILITY_DISABLE, ACCESSIBILITY_ENABLE, AXIS_X, AXIS_Y, CONTOUR_BACKGROUND, CONTOUR_BORDER, CONTOUR_PADDING_BORDER, DEFAULT_SCALE, DRAG_DOWN, DRAG_HORIZONTAL, DRAG_HORIZONTAL_VERTICAL, DRAG_LEFT, DRAG_RIGHT, DRAG_UP, DRAG_VERTICAL, EVENT_TYPE_COMPONENT_ACCESSIBILITY_FOCUSED, EVENT_TYPE_COMPONENT_ACCESSIBILITY_FOCUSED_CLEAR, EVENT_TYPE_COMPONENT_CLICKED, EVENT_TYPE_COMPONENT_FOCUSED, EVENT_TYPE_COMPONENT_LONG_CLICKED, EVENT_TYPE_COMPONENT_SCROLLED, EVENT_TYPE_COMPONENT_SELECTED, EVENT_TYPE_COMPONENT_TEXT_UPDATE, EVENT_TYPE_NOTICE, EVENT_TYPE_NOTIFICATION_STATE_UPDATE, EVENT_TYPE_TOAST_SHOW, EVENT_TYPE_WINDOW_STATE_UPDATE, FOCUS_ADAPTABLE, FOCUS_DISABLE, FOCUS_ENABLE, FOCUS_NEXT, FOCUS_PREVIOUS, FOCUS_SIDE_BOTTOM, FOCUS_SIDE_LEFT, FOCUS_SIDE_RIGHT, FOCUS_SIDE_TOP, HIDE, HORIZONTAL, ID_DEFAULT, INHERITED_MODE, INVISIBLE, mBackgroundElement, mCanvasForTaskOverContent, mCanvasForTaskUnderContent, mComponentParent, mContext, mDrawTaskOverContent, mDrawTaskUnderContent, mEstimateSizeListener, mForegroundElement, mForwardHoverListener, mHoverEventListener, mKeyEventListener, mMouseEventListener, mPosition, mRotationEventListener, mShadowElement, mUnconsumedKeyEventListener, OVAL_MODE, OVAL_SCROLL_BAR_MODE, POSITION_X_INDEX, POSITION_Y_INDEX, RECT_MODE, RECT_SCROLL_BAR_MODE, SCROLL_AUTO_STAGE, SCROLL_IDLE_STAGE, SCROLL_NORMAL_STAGE, UNSPECIFIED_SCROLL_BAR_MODE, VERTICAL, VISIBLE| 构造器 | 说明 |
|---|---|
WebView(Context context) |
A constructor used to create a
WebView instance by passing the context and using the default style
attributes. |
WebView(Context context,
AttrSet attrSet) |
A constructor used to create a
WebView instance by passing the context and a set of style attributes. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
activateGlobalTimers() |
Activates global timers of a
WebView object. |
void |
addJsCallback(String name,
JsCallback jsCallBack) |
Adds the JavaScript object that can be called in the
WebView. |
boolean |
canScroll(int direction,
int distance) |
Checks whether the
WebView content can be scrolled by the specified distance in the horizontal or
vertical direction. |
void |
clearAllCache() |
Clears resource caches of a
WebView object in both the memory and local storage. |
void |
clearMemoryCache() |
Clears the resource cache of a
WebView object in the memory. |
void |
doFling(int horizonalVelocity,
int verticalVelocity) |
Triggers scrolling of a web page.
|
void |
executeJs(String jsContent,
AsyncCallback<String> callback) |
Executes the JavaScript script.
|
boolean |
executeLongClick() |
Performs a long-click event in a component.
|
String |
getCurrentUrl() |
Obtains the URL currently loaded by a
WebView object. |
PixelMap |
getFavicon() |
Obtains the favicon of the current web page.
|
String |
getFirstRequestUrl() |
Obtains the first loaded URL of the current page.
|
FontConfig |
getFontConfig() |
Obtains a
FontConfig instance to configure fonts for a WebView object. |
Component.KeyEventListener |
getKeyEventListener() |
Obtains a listener for monitoring key events of this component.
|
Navigator |
getNavigator() |
Obtains a
Navigator object. |
int |
getProgress() |
Obtains the loading progress of the current web page.
|
ScaleController |
getScaleController() |
Obtains a
ScaleController instance to manage and observe scaling of a WebView object. |
String |
getTitle() |
Obtains the title of the current page.
|
Component.TouchEventListener |
getTouchEventListener() |
Obtains a listener for monitoring touch events of this component.
|
WebConfig |
getWebConfig() |
Obtains the
WebConfig object, which is used to set the WebView attributes. |
int |
getWebPageHeight() |
Obtains the height of a web page.
|
void |
inactivateGlobalTimers() |
Stops global timers of a
WebView object. |
void |
load(String url) |
Loads the given web page linked by a specified URL.
|
void |
load(String data,
String mimeType,
boolean isBase64Encoded) |
Loads data strings to a
WebView object. |
void |
load(String data,
String mimeType,
String encodingName,
String baseUrl,
String virtualUrl) |
Loads the data schema to the
WebView object with the base URL and virtual URL specified. |
void |
notifyJsOnline(boolean isOnline) |
Notifies a
WebView object of the current network status. |
void |
onActive() |
Called when the
WebView is set to be active, for example, when the page containing this WebView
regains the focus or the page is being displayed in the foreground. |
void |
onInactive() |
Called when the
WebView is set to be inactive, for example, when the page containing this WebView
has lost focus or no operation is allowed on the page. |
void |
onStop() |
Called when the
WebView is set to be stopped, for example, when the page containing this WebView
is destroyed. |
boolean |
pageDown(boolean isToBottom) |
Scrolls down a web page.
|
boolean |
pageUp(boolean isToTop) |
Scrolls up the web page.
|
void |
post(String url,
byte[] data) |
Sends a POST request to a specified URL.
|
void |
reload() |
Reloads the current page.
|
void |
removeJsCallback(String name) |
Removes the JavaScript callback object.
|
boolean |
requestFocus() |
Requests focus for the current component or one of its child components.
|
void |
setBrowserAgent(BrowserAgent browserAgent) |
Sets a specified
BrowserAgent object, which is used to process the web-page-related callbacks, for
example, processing JavaScript events and website attribute change events. |
void |
setFocusChangedListener(Component.FocusChangedListener listener) |
Registers a listener for focus change events in the component.
|
void |
setKeyEventListener(Component.KeyEventListener listener) |
Registers a listener for key events in the component.
|
void |
setTouchEventListener(Component.TouchEventListener listener) |
Registers a listener for touch events in the component.
|
void |
setVisibility(int visibility) |
Sets the visibility of a component.
|
void |
setWebAgent(WebAgent webAgent) |
Sets a specified
WebAgent object, which is used to process notifications and requests. |
void |
setWebViewBackground(RgbColor rgbColor) |
Sets the background color of the
WebView object. |
void |
stopLoading() |
Stops loading of data to a
WebView object. |
addDrawTask, addDrawTask, addScrolledListener, announceAccessibility, arrange, bindCornerMark, callOnClick, canScroll, clearFocus, createAnimatorProperty, disableFadeEffect, dispatchRestoreState, dispatchSaveState, draw, enableCornerMark, enableFadeEffect, enableSaveState, enableScrollBar, estimateSize, executeDoubleClick, findComponentById, findComponentHolderById, findFocus, findNextFocusableComponent, findRequestNextFocus, getAccessibility, getAccessibilityDescription, getAlpha, getAltitude, getBackgroundElement, getBaseLine, getBottom, getBoundaryFadeEffectBottomRate, getBoundaryFadeEffectLeftRate, getBoundaryFadeEffectRightRate, getBoundaryFadeEffectTopRate, getCenterZoomFactor, getCentralScrollMode, getClickedListener, getClipEnabled, getComponentDescription, getComponentMinSize, getComponentParent, getComponentPosition, getComponentSize, getComponentStateChangedListener, getComponentTreeObserver, getContentEnable, getContentPosition, getContentPositionX, getContentPositionY, getContext, getContour, getContourRefreshedListener, getCornerMark, getDragAcceptAngle, getDraggedListener, getEditEventListener, getEstimatedHeight, getEstimatedWidth, getFadeEffectBoundaryWidth, getFadeEffectColor, getFocusable, getFocusBorderEnable, getFocusBorderPadding, getFocusBorderRadius, getFocusBorderWidth, getForegroundElement, getForegroundGravity, getForwardHoverListener, getForwardTouchListener, getGesturePriority, getHeight, getHorizontalPadding, getHorizontalPosition, getHoverEventListener, getId, getLayoutConfig, getLayoutDirection, getLayoutDirectionResolved, getLayoutRefreshedListener, getLeft, getLocationOnScreen, getMarginBottom, getMarginLeft, getMarginRight, getMargins, getMarginsLeftAndRight, getMarginsTopAndBottom, getMarginTop, getMatrix, getMinHeight, getMinWidth, getMode, getModeResolved, getMouseEventListener, getMousePointerStyleListener, getName, getPadding, getPaddingBottom, getPaddingEnd, getPaddingLeft, getPaddingRight, getPaddingStart, getPaddingTop, getPivot, getPivotX, getPivotY, getResourceManager, getRight, getRotation, getRotationEventListener, getRotationSensitivity, getScale, getScaledListener, getScaleX, getScaleY, getScrollbarBackgroundColor, getScrollbarColor, getScrollbarFadingDelay, getScrollbarFadingDuration, getScrollBarMode, getScrollbarRadius, getScrollbarRoundRect, getScrollbarStartAngle, getScrollbarSweepAngle, getScrollbarThickness, getScrollState, getScrollValue, getSelfVisibleRect, getShadowColor, getSpeechEventListener, getTag, getTop, getTransitionSymbol, getTranslation, getTranslationX, getTranslationY, getTranslationZ, getUserNextFocus, getVerticalPadding, getVerticalPosition, getVisibility, getVisibleRectOnScreen, getWidth, getWindowVisibleRect, hasFocus, hasFocusedEffect, hasOverlaps, hideFocusBorder, informConfigurationChanged, invalidate, isBoundaryFadeEffectEnable, isBoundToWindow, isClickable, isClipToContourEnabled, isComponentDisplayed, isEnabled, isFadeEffected, isFocusable, isFocused, isIdentityMatrix, isLongClickOn, isPressed, isRtl, isSaveStateEnabled, isScrollbarFadingOn, isScrollBarOn, isScrollbarOverlapEnabled, isSelected, isSoundEnable, isTouchFocusable, isVibrationEffectEnabled, notifyAccessibility, onAttributeConfigChanged, onDrag, onRestoreState, onRtlChanged, onSaveState, performScale, playSound, postLayout, refreshContour, release, removeBindStateChangedListener, removeScrolledListener, requestForceForwardTouchEvent, restoreComponentTreeState, saveComponentTreeState, scrollBy, scrollTo, setAccessibility, setAccessibilityDescription, setAlpha, setAltitude, setBackground, setBindStateChangedListener, setBottom, setBoundaryFadeEffectEnable, setCanAcceptScrollListener, setCenterZoomFactor, setCentralScrollMode, setClickable, setClickedListener, setClipEnabled, setClipToContourEnabled, setComponentDescription, setComponentMinSize, setComponentPosition, setComponentPosition, setComponentSize, setComponentStateChangedListener, setContentEnable, setContentPosition, setContentPositionX, setContentPositionY, setContour, setContourRefreshedListener, setDoubleClickedListener, setDragAcceptAngle, setDraggedListener, setEditEventListener, setEnabled, setEstimatedSize, setEstimateSizeListener, setFadeEffectBoundaryWidth, setFadeEffectColor, setFocusable, setFocusBorderEnable, setFocusBorderPadding, setFocusBorderRadius, setFocusBorderRadius, setFocusBorderWidth, setFocusedEffect, setForeground, setForegroundGravity, setForwardHoverListener, setForwardTouchListener, setGesturePriority, setHeight, setHorizontalPadding, setHorizontalPosition, setHoverEventListener, setId, setLayoutConfig, setLayoutDirection, setLayoutRefreshedListener, setLeft, setLongClickable, setLongClickedListener, setMarginBottom, setMarginLeft, setMarginRight, setMarginsLeftAndRight, setMarginsTopAndBottom, setMarginTop, setMinHeight, setMinWidth, setMode, setMouseEventListener, setMousePointerStyleListener, setName, setOnDragListener, setPadding, setPaddingBottom, setPaddingLeft, setPaddingRelative, setPaddingRight, setPaddingTop, setPivot, setPivot, setPivotX, setPivotY, setPosition, setPositionX, setPositionY, setPressState, setRight, setRotation, setRotationEventListener, setRotationSensitivity, setScale, setScaledListener, setScaleX, setScaleY, setScrollbarBackgroundColor, setScrollbarColor, setScrollbarFadingDelay, setScrollbarFadingDuration, setScrollbarFadingEnabled, setScrollBarMode, setScrollbarOverlapEnabled, setScrollbarRadius, setScrollbarRoundRect, setScrollbarStartAngle, setScrollbarSweepAngle, setScrollbarThickness, setScrolledListener, setSelected, setShadowColor, setShadowStyle, setSoundEnable, setSpeechEventListener, setTag, setTop, setTouchFocusable, setTransitionSymbol, setTranslation, setTranslationX, setTranslationY, setTranslationZ, setUnconsumedKeyEventListener, setUserNextFocus, setVerticalPadding, setVerticalPosition, setVibrationEffectEnabled, setWidth, showFocusBorder, simulateClick, simulateDrag, startDragAndDrop, subscribeVoiceEvents, unsubscribeVoiceEventspublic WebView(Context context)
WebView instance by passing the context and using the default style
attributes.context - Indicates the context to use.public void setKeyEventListener(Component.KeyEventListener listener)
ComponentAll registered observers will be notified of key events dispatched to the component.
setKeyEventListener 在类中 Componentlistener - Indicates the listener for key events.public Component.KeyEventListener getKeyEventListener()
ComponentgetKeyEventListener 在类中 Componentpublic void setTouchEventListener(Component.TouchEventListener listener)
ComponentAll registered observers will be notified of touch events dispatched to the component.
setTouchEventListener 在类中 Componentlistener - Indicates the listener for touch events.public Component.TouchEventListener getTouchEventListener()
ComponentgetTouchEventListener 在类中 Componentpublic void setFocusChangedListener(Component.FocusChangedListener listener)
All registered observers will be notified of focus change events dispatched to the component.
setFocusChangedListener 在类中 Componentlistener - Indicates the listener for focus change events.public void setWebViewBackground(RgbColor rgbColor)
WebView object.rgbColor - Indicates the background color to set.public void load(String url)
url - Indicates the URL to load.public void load(String data, String mimeType, boolean isBase64Encoded)
WebView object.
If you need to avoid the JavaScript same-origin policy, use the
load(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String) method instead.
data - Indicates the data to be loaded. The value cannot be null. If the data is not encoded using
Base64, ensure that all characters in the data string comply with the URL specifications.mimeType - Indicates the MIME type of the data. If the value is null, text/html is used by
default.isBase64Encoded - Specifies whether the data is encoded using Base64.public void load(String data, String mimeType, String encodingName, String baseUrl, String virtualUrl)
WebView object with the base URL and virtual URL specified.
When the data schema is used by the base URL, data must be URL or Base64 encoded. Otherwise, the data will be loaded as a pure character string and the URL-encoded entity will not be decoded.
data - Indicates the data to be loaded. The value cannot be null.mimeType - Indicates the MIME type of the data. If the value is null, text/html is used by
default.encodingName - Indicates the data encoding format. The value cannot be null.baseUrl - Indicates the base URL for loading data. The value may be null. The default value is
about:blank.virtualUrl - Indicates the URL displayed to users, which may be null. The default value is
about:blank. This parameter is valid only when the data schema is not used by the base URL.public void reload()
public String getFirstRequestUrl()
public String getTitle()
public void onInactive()
WebView is set to be inactive, for example, when the page containing this WebView
has lost focus or no operation is allowed on the page.public void onActive()
WebView is set to be active, for example, when the page containing this WebView
regains the focus or the page is being displayed in the foreground.public void onStop()
WebView is set to be stopped, for example, when the page containing this WebView
is destroyed.public void executeJs(String jsContent, AsyncCallback<String> callback)
jsContent - Indicates the script to be executed.callback - Indicates the asynchronous callback object.public void addJsCallback(String name, JsCallback jsCallBack)
WebView.name - Indicates the object name. This parameter cannot be null.jsCallBack - Indicates the callback object.public void removeJsCallback(String name)
name - Indicates the callback object name.public void stopLoading()
WebView object.public int getProgress()
public void post(String url, byte[] data)
load(java.lang.String) method.url - Indicates the URL for loading. The value cannot be null.data - Indicates the data content for the POST request. The value must be encoded using
application/x-www-form-urlencoded and cannot be null.public void doFling(int horizonalVelocity,
int verticalVelocity)
horizonalVelocity - Indicates the horizontal velocity.verticalVelocity - Indicates the vertical velocity.public int getWebPageHeight()
public PixelMap getFavicon()
null.public String getCurrentUrl()
WebView object.public boolean pageDown(boolean isToBottom)
isToBottom - Indicates whether to scroll a web page to its bottom. Value true indicates scroll-down
to the bottom of the web page; value false indicates scroll-down to the half height of the web page.true if the page is scrolled down to its bottom; returns false otherwise.public boolean pageUp(boolean isToTop)
isToTop - Specifies whether to scroll a web page to its top. Value true indicates scroll-up to the
top of the web page; value false indicates scroll-up to the half height of the web page.true if the page is scrolled up to its top; returns false otherwise.public void clearMemoryCache()
WebView object in the memory.public void clearAllCache()
WebView object in both the memory and local storage.public void notifyJsOnline(boolean isOnline)
WebView object of the current network status.isOnline - Specifies whether the current network is online.public boolean canScroll(int direction,
int distance)
WebView content can be scrolled by the specified distance in the horizontal or
vertical direction.direction - Indicates the X-axis or Y-axis direction. Component#AXIS_X indicates the X-axis
direction (horizontal), and Component#AXIS_Y indicates the Y-axis direction (vertical).distance - Indicates the offset distance in the specified direction. A negative value indicates upward or
leftward, and a positive value indicates downward or rightward.true if the WebView content can be scrolled by the specified distance in the
given direction; returns false otherwise.public void inactivateGlobalTimers()
WebView object.public void activateGlobalTimers()
WebView object.public WebConfig getWebConfig()
WebConfig object, which is used to set the WebView attributes.WebConfig object.public Navigator getNavigator()
Navigator object.Navigator object.public ScaleController getScaleController()
ScaleController instance to manage and observe scaling of a WebView object.ScaleController instance.public FontConfig getFontConfig()
FontConfig instance to configure fonts for a WebView object.FontConfig instance.public void setWebAgent(WebAgent webAgent)
WebAgent object, which is used to process notifications and requests.webAgent - Indicates the specified WebAgent object.public void setBrowserAgent(BrowserAgent browserAgent)
BrowserAgent object, which is used to process the web-page-related callbacks, for
example, processing JavaScript events and website attribute change events.browserAgent - Indicates the specified BrowserAgent object.public void setVisibility(int visibility)
Component
When the parameter value is Component.VISIBLE, the component occupies space and needs to be rendered.
When the parameter value is Component.INVISIBLE, the component occupies space but does not need to be rendered.
When the parameter value is Component.HIDE, the component occupies no space and does not need to be rendered.
setVisibility 在类中 Componentvisibility - The value is one of the following: Component.VISIBLE, Component.INVISIBLE, or Component.HIDE.public boolean requestFocus()
Component
A component will not be able to actually take focus in any of the following situations:
the component is not focusable (Component.isFocusable() returns false); or the component is not focusable
in touch mode (Component.isTouchFocusable() returns false); or the component is invisible,
disabled, or has not acquired space for drawing.
requestFocus 在类中 Componenttrue if the component or one of its child components has taken focus; returns
false otherwise.public boolean executeLongClick()
ComponentThis method calls the OnLongClickListener created for the component. Observers registered by the
Component.setLongClickedListener(ohos.agp.components.Component.LongClickedListener) method will be notified. After the callback is
executed, the system passes a lifting event to the long-click event listener.
executeLongClick 在类中 Componenttrue if the callback function is executed successfully.