Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
ab7d36b0
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看板
未验证
提交
ab7d36b0
编写于
8月 18, 2023
作者:
O
openharmony_ci
提交者:
Gitee
8月 18, 2023
浏览文件
操作
浏览文件
下载
差异文件
!22464 同步22380修改,输入法应用开发指南中需要更新示例代码
Merge pull request !22464 from ningning/cherry-pick-1692323348
上级
19611f34
d1ecd078
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
73 addition
and
73 deletion
+73
-73
zh-cn/application-dev/application-models/inputmethodextentionability.md
...ion-dev/application-models/inputmethodextentionability.md
+73
-73
未找到文件。
zh-cn/application-dev/application-models/inputmethodextentionability.md
浏览文件 @
ab7d36b0
...
...
@@ -56,14 +56,12 @@
```
ts
import
InputMethodExtensionAbility
from
'
@ohos.InputMethodExtensionAbility
'
;
import
{
KeyboardController
}
from
'
./model/KeyboardController
'
import
keyboardController
from
'
./model/KeyboardController
'
export
default
class
InputDemoService
extends
InputMethodExtensionAbility
{
private
keyboardController
:
KeyboardController
;
onCreate
(
want
)
{
this
.
keyboardController
=
new
KeyboardController
(
this
.
context
);
this
.
keyboardController
.
onCreate
();
// 初始化窗口并注册对输入法框架的事件监听
keyboardController
.
onCreate
(
this
.
context
);
// 初始化窗口并注册对输入法框架的事件监听
}
onDestroy
()
{
...
...
@@ -72,32 +70,29 @@
}
}
```
2.
KeyboardController.ts文件。
```
ts
import
common
from
'
@ohos.app.ability.common
'
;
import
inputMethodEngine
from
'
@ohos.inputMethodEngine
'
;
import
display
from
'
@ohos.display
'
;
import
windowManager
from
'
@ohos.window
'
;
// 调用输入法框架的getInputMethodAbility方法获取实例,并由此实例调用输入法框架功能接口
globalThis
.
input
Ability
=
inputMethodEngine
.
getInputMethodAbility
();
const
inputMethodAbility
:
inputMethodEngine
.
InputMethod
Ability
=
inputMethodEngine
.
getInputMethodAbility
();
export
class
KeyboardController
{
mContext
;
// 保存InputMethodExtensionAbility中的context属性
WINDOW_TYPE_INPUT_METHOD_FLOAT
=
2105
;
// 定义窗口类型,2105代表输入法窗口类型,用于创建输入法应用窗口
windowName
=
'
inputApp
'
;
private
windowHeight
:
number
=
0
;
private
windowWidth
:
number
=
0
;
private
nonBarPosition
:
number
=
0
;
private
isWindowShowing
:
boolean
=
false
;
private
mContext
:
common
.
ExtensionContext
|
undefined
=
undefined
;
// 保存InputMethodExtensionAbility中的context属性
private
panel
:
inputMethodEngine
.
Panel
|
undefined
=
undefined
;
private
textInputClient
:
inputMethodEngine
.
InputClient
|
undefined
=
undefined
;
private
keyboardController
:
inputMethodEngine
.
KeyboardController
|
undefined
=
undefined
;
constructor
(
context
)
{
this
.
mContext
=
context
;
constructor
()
{
}
public
onCreate
():
void
public
onCreate
(
context
:
common
.
ExtensionContext
):
void
{
this
.
mContext
=
context
;
this
.
initWindow
();
// 初始化窗口
this
.
registerListener
();
// 注册对输入法框架的事件监听
}
...
...
@@ -105,80 +100,88 @@
public
onDestroy
():
void
// 应用生命周期销毁
{
this
.
unRegisterListener
();
// 去注册事件监听
let
win
=
windowManager
.
findWindow
(
this
.
windowName
);
win
.
destroyWindow
();
// 销毁窗口
if
(
this
.
panel
)
{
// 销毁窗口
this
.
panel
.
hide
();
inputMethodAbility
.
destroyPanel
(
this
.
panel
);
}
}
public
insertText
(
text
:
string
):
void
{
if
(
this
.
textInputClient
)
{
this
.
textInputClient
.
insertText
(
text
);
}
}
public
deleteForward
(
length
:
number
):
void
{
if
(
this
.
textInputClient
)
{
this
.
textInputClient
.
deleteForward
(
length
);
}
}
private
initWindow
():
void
// 初始化窗口
{
if
(
this
.
mContext
===
undefined
)
{
return
;
}
let
dis
=
display
.
getDefaultDisplaySync
();
let
dWidth
=
dis
.
width
;
let
dHeight
=
dis
.
height
;
let
keyHeightRate
=
0.47
;
let
keyHeight
=
dHeight
*
keyHeightRate
;
this
.
windowWidth
=
dWidth
;
this
.
windowHeight
=
keyHeight
;
this
.
nonBarPosition
=
dHeight
-
keyHeight
;
let
config
=
{
name
:
this
.
windowName
,
windowType
:
this
.
WINDOW_TYPE_INPUT_METHOD_FLOAT
,
ctx
:
this
.
mContext
}
windowManager
.
createWindow
(
config
).
then
((
win
)
=>
{
// 根据窗口类型创建窗口
win
.
resize
(
dWidth
,
keyHeight
).
then
(()
=>
{
win
.
moveWindowTo
(
0
,
this
.
nonBarPosition
).
then
(()
=>
{
win
.
setUIContent
(
'
pages/InputMethodExtAbility/Index
'
).
then
(()
=>
{
});
});
});
let
nonBarPosition
=
dHeight
-
keyHeight
;
let
panelInfo
:
inputMethodEngine
.
PanelInfo
=
{
type
:
inputMethodEngine
.
PanelType
.
SOFT_KEYBOARD
,
flag
:
inputMethodEngine
.
PanelFlag
.
FLG_FLOATING
};
inputMethodAbility
.
createPanel
(
this
.
mContext
,
panelInfo
).
then
(
async
(
inputPanel
:
inputMethodEngine
.
Panel
)
=>
{
this
.
panel
=
inputPanel
;
if
(
this
.
panel
)
{
await
this
.
panel
.
resize
(
dWidth
,
keyHeight
);
await
this
.
panel
.
mobeTo
(
0
,
nonBarPosition
);
await
this
.
panel
.
setUiContent
(
'
inputmethodextability/pages/Index
'
);
}
});
}
private
registerListener
():
void
{
this
.
registerInputListener
();
// 注册对输入法框架服务的监听
globalThis
.
inputAbility
.
on
(
'
keyboardShow
'
,
()
=>
{
// 注册显示键盘事件监听
if
(
this
.
isWindowShowing
)
{
return
;
}
this
.
isWindowShowing
=
true
;
this
.
showHighWindow
();
// 显示窗口
});
...
// 注册隐藏键盘事件监听等
}
private
registerInputListener
()
{
// 注册对输入法框架服务的开启及停止事件监听
globalThis
.
input
Ability
.
on
(
'
inputStart
'
,
(
kbController
,
textInputClient
)
=>
{
globalT
his
.
textInputClient
=
textInputClient
;
// 此为输入法客户端实例,由此调用输入法框架提供给输入法应用的功能接口
globalThis
.
key
boardController
=
kbController
;
private
registerInputListener
()
:
void
{
// 注册对输入法框架服务的开启及停止事件监听
inputMethod
Ability
.
on
(
'
inputStart
'
,
(
kbController
,
textInputClient
)
=>
{
t
his
.
textInputClient
=
textInputClient
;
// 此为输入法客户端实例,由此调用输入法框架提供给输入法应用的功能接口
this
.
boardController
=
kbController
;
})
globalThis
.
inputAbility
.
on
(
'
inputStop
'
,
(
imeId
)
=>
{
if
(
imeId
==
"
包名/Ability名
"
)
{
this
.
mContext
.
destroy
();
// 销毁InputMethodExtensionAbility服务
}
globalThis
.
inputAbility
.
on
(
'
inputStop
'
,
()
=>
{
this
.
onDestroy
();
// 销毁KeyboardController
});
}
private
unRegisterListener
():
void
{
globalThis
.
inputAbility
.
off
(
'
inputStart
'
);
globalThis
.
inputAbility
.
off
(
'
inputStop
'
,
()
=>
{});
globalThis
.
inputAbility
.
off
(
'
keyboardShow
'
);
inputMethodAbility
.
off
(
'
inputStart
'
);
inputMethodAbility
.
off
(
'
inputStop
'
,
()
=>
{});
}
private
showHighWindow
()
{
let
win
=
windowManager
.
findWindow
(
this
.
windowName
)
win
.
resize
(
this
.
windowWidth
,
this
.
windowHeight
).
then
(()
=>
{
win
.
moveWindowTo
(
0
,
this
.
nonBarPosition
).
then
(()
=>
{
win
.
showWindow
().
then
(()
=>
{
this
.
isWindowShowing
=
false
;
})
})
})
private
async
showHighWindow
():
Promise
<
void
>
{
try
{
if
(
this
.
panel
)
{
await
this
.
panel
.
resize
(
this
.
windowWidth
,
this
.
windowHeight
);
await
this
.
panel
.
moveTo
(
0
,
this
.
nonBarPosition
);
await
this
.
panel
.
show
();
}
}
catch
(
e
)
{
console
.
log
(
'
err occur:
'
+
JSON
.
stringify
(
e
));
}
}
}
const
keyboardController
=
new
KeyboardController
();
export
default
keyboardController
;
```
3.
KeyboardKeyData.ts文件。
...
...
@@ -231,7 +234,8 @@
同时在resources/base/profile/main_pages.json文件的src字段中添加此文件路径。
```
ets
import { numberSourceListData, sourceListType } from './keyboardKeyData'
import { numberSourceListData, sourceListType } from './keyboardKeyData';
import keyboardController from '../model/KeyboardController';
@Component
struct keyItem {
...
...
@@ -250,10 +254,8 @@
.borderRadius(6)
.width("8%")
.height("65%")
.onTouch((event: TouchEvent) => {
if (event.type === TouchType.Down) {
globalThis.textInputClient.insertText(this.keyValue.content);
}
.onClick(() => {
keyboardController.insertText(this.keyValue.content);
})
}
}
...
...
@@ -274,10 +276,8 @@
.backgroundColor(this.keyBgc)
.width("13%")
.borderRadius(6)
.onTouch((event: TouchEvent) => {
if (event.type === TouchType.Down) {
globalThis.textInputClient.deleteForward(1);
}
.onClick(() => {
keyboardController.deleteForward(1);
})
}
}
...
...
@@ -338,7 +338,7 @@
}
}
```
5.
在工程Module对应的
[
module.json5配置文件
](
../quick-start/module-configuration-file.md
)
中注册InputMethodExtensionAbility,type标签需要设置为“inputMethod”,srcEntry标签表示当前InputMethodExtensionAbility组件所对应的代码路径。
```
ts
...
...
@@ -410,4 +410,4 @@
针对InputMethodExtensionAbility开发,有以下相关实例可供参考:
-
[
Kika输入法
](
https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/InputMethod/KikaInput
)
\ No newline at end of file
-
[
Kika输入法
](
https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/InputMethod/KikaInput
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录