Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
3c3a3630
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
3c3a3630
编写于
8月 02, 2022
作者:
O
openharmony_ci
提交者:
Gitee
8月 02, 2022
浏览文件
操作
浏览文件
下载
差异文件
!7587 翻译完成 6413:ts-universal-attributes-component-id.md
Merge pull request !7587 from ester.zhou/TR-6413
上级
c5950fb9
e5ee2679
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
201 addition
and
42 deletion
+201
-42
en/application-dev/reference/arkui-ts/ts-universal-attributes-component-id.md
...eference/arkui-ts/ts-universal-attributes-component-id.md
+201
-42
未找到文件。
en/application-dev/reference/arkui-ts/ts-universal-attributes-component-id.md
浏览文件 @
3c3a3630
# Component ID
**id**
identifies a component uniquely within an application. This module provides APIs for obtaining the attributes of or sending events to the component with the specified ID.
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
> **NOTE**
>
> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
## Required Permissions
...
...
@@ -12,93 +14,250 @@ None
## Attributes
| Name | Type | Default Value | Description |
| Name| Type| Default Value| Description|
| -------- | -------- | -------- | -------- |
| id | string | '' | Unique ID
of the component. The uniqueness is ensured by the user. |
| id | string | '' | Unique ID
you assigned to the component.|
## APIs
### getInspectorByKey
### getInspectorByKey
<sup>9+</sup>
getInspectorByKey(id: string): string
Obtains all attributes of the component with the specified ID, excluding the information about child components.
-
Parameters
| Name | Type | Mandatory | Default Value | Description |
| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| id | string | Yes
| - | ID of the component whose attributes are to be obtained. |
| id | string | Yes
| - | ID of the component whose attributes are to be obtained.|
-
Return value
| Type | Description |
| Type| Description|
| -------- | -------- |
| string | JSON string of the component attribute list.
|
| string | JSON string of the component attribute list.
|
### getInspectorTree<sup>9+</sup>
### sendEventByKey
getInspectorTree(): string
Obtains the component tree and component attributes.
-
Return value
| Type | Description |
| ------ | ---------------------------------- |
| string | JSON string of the component tree and component attribute list.|
### sendEventByKey<sup>9+</sup>
sendEventByKey(id: string, action: number, params: string): boolean
Sends an event to the component with the specified ID.
-
Parameters
| Name | Type | Mandatory | Default Value | Description |
| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| id | string | Yes
| - | ID of the component for which the event is to be sent. |
| action | number | Yes
| - | Type of the event to be sent. The options are as follows:
<br/>
- Click event: 10.
<br/>
- LongClick: 11. |
| params | string | Yes
| - | Event parameters. If there is no parameter, pass an empty string
**""**
. |
| id | string | Yes
| - | ID of the component to which the event is to be sent.|
| action | number | Yes
| - | Type of the event to be sent. The options are as follows:
<br>
-
**10**
: click event.
<br>
-
**11**
: long-click event.|
| params | string | Yes
| - | Event parameters. If there is no parameter, pass an empty string
**""**
.|
-
Return value
| Type | Description |
| Type| Description|
| -------- | -------- |
| boolean | Returns
**false**
if the component with the specified ID cannot be found; returns
**true**
otherwise. |
| boolean | Returns
**true**
if the component with the specified ID is found; returns
**false**
otherwise.|
### sendTouchEvent<sup>9+</sup>
sendTouchEvent(event: TouchObject): boolean
Sends a touch event.
-
Parameters
| Name | Type | Mandatory| Default Value| Description |
| ----- | ----------- | ---- | ------ | ------------------------------------------------------------ |
| event | TouchObject | Yes | - | Location where a touch event is triggered. For details, see
[
TouchEvent
](
ts-universal-events-touch.md#touchevent
)
.|
-
Return value
| Type | Description |
| ------- | ------------------------------------------- |
| boolean | Returns
**true**
if the event is sent successfully; returns
**false**
otherwise.|
### sendKeyEvent<sup>9+</sup>
sendKeyEvent(event: KeyEvent): boolean
Sends a key event.
-
Parameters
| Name | Type | Mandatory| Default Value| Description |
| ----- | -------- | ---- | ------ | ------------------------------------------------------------ |
| event | KeyEvent | Yes | - | Key event. For details, see
[
KeyEvent
](
ts-universal-events-key.md#keyevent
)
.|
-
Return value
| Type | Description |
| ------- | --------------------------------------------- |
| boolean | Returns
**true**
if the event is sent successfully; returns
**false**
otherwise.|
### sendMouseEvent<sup>9+</sup>
sendMouseEvent(event: MouseEvent): boolean
Sends a mouse event.
-
Parameters
| Name | Type | Mandatory| Default Value| Description |
| ----- | ---------- | ---- | ------ | ------------------------------------------------------------ |
| event | MouseEvent | Yes | - | Mouse event. For details, see
[
MouseEvent
](
ts-universal-mouse-key.md#mouseevent
)
.|
-
Return value
| Type | Description |
| ------- | --------------------------------------------- |
| boolean | Returns
**true**
if the event is sent successfully; returns
**false**
otherwise.|
## Example
```
```
ts
// xxx.ets
class
Utils
{
static
rect_left
;
static
rect_top
;
static
rect_right
;
static
rect_bottom
;
static
rect_value
;
static
getComponentRect
(
key
)
{
let
strJson
=
getInspectorByKey
(
key
);
let
obj
=
JSON
.
parse
(
strJson
);
console
.
info
(
"
[getInspectorByKey] current component obj is:
"
+
JSON
.
stringify
(
obj
));
let
rectInfo
=
JSON
.
parse
(
'
[
'
+
obj
.
$rect
+
'
]
'
)
console
.
info
(
"
[getInspectorByKey] rectInfo is:
"
+
rectInfo
);
this
.
rect_left
=
JSON
.
parse
(
'
[
'
+
rectInfo
[
0
]
+
'
]
'
)[
0
]
this
.
rect_top
=
JSON
.
parse
(
'
[
'
+
rectInfo
[
0
]
+
'
]
'
)[
1
]
this
.
rect_right
=
JSON
.
parse
(
'
[
'
+
rectInfo
[
1
]
+
'
]
'
)[
0
]
this
.
rect_bottom
=
JSON
.
parse
(
'
[
'
+
rectInfo
[
1
]
+
'
]
'
)[
1
]
return
this
.
rect_value
=
{
"
left
"
:
this
.
rect_left
,
"
top
"
:
this
.
rect_top
,
"
right
"
:
this
.
rect_right
,
"
bottom
"
:
this
.
rect_bottom
}
}
}
@
Entry
@
Component
struct
IdExample
{
@
State
text
:
string
=
''
build
()
{
Flex
({
direction
:
FlexDirection
.
Column
,
alignItems
:
ItemAlign
.
Center
,
justifyContent
:
FlexAlign
.
Center
})
{
Button
()
{
Text('click')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
}).onClick(() => {
Text
(
'
onKeyTab
'
).
fontSize
(
25
).
fontWeight
(
FontWeight
.
Bold
)
}.
margin
({
top
:
20
}).
backgroundColor
(
'
#0D9FFB
'
)
.
onKeyEvent
(()
=>
{
this
.
text
=
"
onKeyTab
"
})
Button
()
{
Text
(
'
click to start
'
).
fontSize
(
25
).
fontWeight
(
FontWeight
.
Bold
)
}.
margin
({
top
:
20
})
.
onClick
(()
=>
{
console
.
info
(
getInspectorByKey
(
"
click
"
))
console
.
info
(
getInspectorTree
())
this
.
text
=
"
Button 'click to start' is clicked
"
setTimeout
(()
=>
{
sendEventByKey("long
c
lick", 11, "")
sendEventByKey
(
"
long
C
lick
"
,
11
,
""
)
},
2000
)
}).
id
(
'
click
'
)
Button
()
{
Text('longclick')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor('#0D9FFB')
Text
(
'
longClick
'
).
fontSize
(
25
).
fontWeight
(
FontWeight
.
Bold
)
}.
margin
({
top
:
20
}).
backgroundColor
(
'
#0D9FFB
'
)
.
gesture
(
LongPressGesture().onActionEnd(() => {
console.info('long clicked')
}))
.id('longclick')
LongPressGesture
().
onActionEnd
(()
=>
{
console
.
info
(
'
long clicked
'
)
this
.
text
=
"
Button 'longClick' is longclicked
"
setTimeout
(()
=>
{
let
rect
=
Utils
.
getComponentRect
(
'
onTouch
'
)
let
touchPoint
:
TouchObject
=
{
id
:
1
,
x
:
rect
.
left
+
(
rect
.
right
-
rect
.
left
)
/
2
,
y
:
rect
.
top
+
(
rect
.
bottom
-
rect
.
top
)
/
2
,
type
:
TouchType
.
Down
,
screenX
:
rect
.
left
+
(
rect
.
right
-
rect
.
left
)
/
2
,
screenY
:
rect
.
left
+
(
rect
.
right
-
rect
.
left
)
/
2
,
}
sendTouchEvent
(
touchPoint
)
touchPoint
.
type
=
TouchType
.
Up
sendTouchEvent
(
touchPoint
)
},
2000
)
})).
id
(
'
longClick
'
)
Button
()
{
Text
(
'
onTouch
'
).
fontSize
(
25
).
fontWeight
(
FontWeight
.
Bold
)
}.
type
(
ButtonType
.
Capsule
).
margin
({
top
:
20
})
.
onClick
(()
=>
{
console
.
info
(
'
onTouch is clicked
'
)
this
.
text
=
"
Button 'onTouch' is clicked
"
setTimeout
(()
=>
{
let
rect
=
Utils
.
getComponentRect
(
'
onMouse
'
)
let
mouseEvent
:
MouseEvent
=
{
button
:
MouseButton
.
Left
,
action
:
MouseAction
.
Press
,
x
:
rect
.
left
+
(
rect
.
right
-
rect
.
left
)
/
2
,
y
:
rect
.
top
+
(
rect
.
bottom
-
rect
.
top
)
/
2
,
screenX
:
rect
.
left
+
(
rect
.
right
-
rect
.
left
)
/
2
,
screenY
:
rect
.
top
+
(
rect
.
bottom
-
rect
.
top
)
/
2
,
timestamp
:
1
,
target
:
{
area
:
{
width
:
1
,
height
:
1
,
position
:
{
x
:
1
,
y
:
1
},
globalPosition
:
{
x
:
1
,
y
:
1
}
}
},
source
:
SourceType
.
Mouse
}
sendMouseEvent
(
mouseEvent
)
},
2000
)
}).
id
(
'
onTouch
'
)
Button
()
{
Text
(
'
onMouse
'
).
fontSize
(
25
).
fontWeight
(
FontWeight
.
Bold
)
}.
margin
({
top
:
20
}).
backgroundColor
(
'
#0D9FFB
'
)
.
onMouse
(()
=>
{
console
.
info
(
'
onMouse
'
)
this
.
text
=
"
Button 'onMouse' in onMouse
"
setTimeout
(()
=>
{
let
keyEvent
:
KeyEvent
=
{
type
:
KeyType
.
Down
,
keyCode
:
2049
,
keyText
:
'
tab
'
,
keySource
:
4
,
deviceId
:
0
,
metaKey
:
0
,
timestamp
:
0
}
sendKeyEvent
(
keyEvent
)
},
2000
)
}).
id
(
'
onMouse
'
)
Text
(
this
.
text
).
fontSize
(
25
).
padding
(
15
)
}
.width('100%')
.height('100%')
.
width
(
'
100%
'
).
height
(
'
100%
'
)
}
}
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录