Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
f6cd1bb5
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,发现更多精彩内容 >>
提交
f6cd1bb5
编写于
8月 16, 2023
作者:
G
guojin31
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
输入法应用开发指南示例代码更新
Signed-off-by:
N
guojin31
<
guojin31@huawei.com
>
上级
28e3d32d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
66 addition
and
74 deletion
+66
-74
zh-cn/application-dev/application-models/inputmethodextentionability.md
...ion-dev/application-models/inputmethodextentionability.md
+66
-74
未找到文件。
zh-cn/application-dev/application-models/inputmethodextentionability.md
浏览文件 @
f6cd1bb5
...
...
@@ -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
()
{
...
...
@@ -76,28 +74,26 @@
2.
KeyboardController.ts文件。
```
ts
import
inputMethodEngine
from
'
@ohos.inputMethodEngine
'
;
import
common
from
'
@ohos.app.ability.common
'
;
import
display
from
'
@ohos.display
'
;
import
windowManager
from
'
@ohos.window
'
;
import
inputMethodEngine
from
'
@ohos.inputMethodEngine
'
;
import
InputMethodExtensionContext
from
'
@ohos.inputMethodExtensionContext
'
;
// 调用输入法框架的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
:
InputMethodExtensionContext
|
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
:
InputMethodExtensionContext
):
void
{
this
.
mContext
=
context
;
this
.
initWindow
();
// 初始化窗口
this
.
registerListener
();
// 注册对输入法框架的事件监听
}
...
...
@@ -105,80 +101,79 @@
public
onDestroy
():
void
// 应用生命周期销毁
{
this
.
unRegisterListener
();
// 去注册事件监听
let
win
=
windowManager
.
findWindow
(
this
.
windowName
);
win
.
destroyWindow
();
// 销毁窗口
if
(
this
.
panel
)
{
// 销毁窗口
this
.
panel
.
hide
();
inputMethodAbility
.
destroyPanel
(
this
.
panel
);
}
if
(
this
.
mContext
)
{
this
.
mContext
.
destroy
();
}
}
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
'
);
}
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
;
})
})
})
inputMethodAbility
.
off
(
'
inputStart
'
);
inputMethodAbility
.
off
(
'
inputStop
'
,
()
=>
{});
}
}
const
keyboardController
=
new
KeyboardController
();
export
default
keyboardController
;
```
3.
KeyboardKeyData.ts文件。
...
...
@@ -231,7 +226,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 +246,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 +268,8 @@
.backgroundColor(this.keyBgc)
.width("13%")
.borderRadius(6)
.onTouch((event: TouchEvent) => {
if (event.type === TouchType.Down) {
globalThis.textInputClient.deleteForward(1);
}
.onClick(() => {
keyboardController.deleteForward(1);
})
}
}
...
...
@@ -410,4 +402,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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录