未验证 提交 99446037 编写于 作者: O openharmony_ci 提交者: Gitee

!23976 输入法框架,同步master到0815分支

Merge pull request !23976 from guojin31/monthly_20230815
...@@ -61,29 +61,30 @@ ...@@ -61,29 +61,30 @@
export default class InputDemoService extends InputMethodExtensionAbility { export default class InputDemoService extends InputMethodExtensionAbility {
onCreate(want: Want) { onCreate(want: Want): void {
keyboardController.onCreate(this.context); // 初始化窗口并注册对输入法框架的事件监听 keyboardController.onCreate(this.context); // 初始化窗口并注册对输入法框架的事件监听
} }
onDestroy() { onDestroy(): void {
console.log("onDestroy."); console.log("onDestroy.");
this.keyboardController.onDestroy(); // 销毁窗口并去注册事件监听 keyboardController.onDestroy(); // 销毁窗口并去注册事件监听
} }
} }
``` ```
2. KeyboardController.ts文件。 2. KeyboardController.ts文件。
```ts ```ts
import common from '@ohos.app.ability.common'; import common from '@ohos.app.ability.common';
import inputMethodEngine from '@ohos.inputMethodEngine';
import display from '@ohos.display'; import display from '@ohos.display';
import inputMethodEngine from '@ohos.inputMethodEngine';
import InputMethodExtensionContext from '@ohos.InputMethodExtensionContext';
// 调用输入法框架的getInputMethodAbility方法获取实例,并由此实例调用输入法框架功能接口 // 调用输入法框架的getInputMethodAbility方法获取实例,并由此实例调用输入法框架功能接口
const inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility(); const inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility();
export class KeyboardController { export class KeyboardController {
private mContext: common.ExtensionContext | undefined = undefined; // 保存InputMethodExtensionAbility中的context属性 private mContext: InputMethodExtensionContext | undefined = undefined; // 保存InputMethodExtensionAbility中的context属性
private panel: inputMethodEngine.Panel | undefined = undefined; private panel: inputMethodEngine.Panel | undefined = undefined;
private textInputClient: inputMethodEngine.InputClient | undefined = undefined; private textInputClient: inputMethodEngine.InputClient | undefined = undefined;
private keyboardController: inputMethodEngine.KeyboardController | undefined = undefined; private keyboardController: inputMethodEngine.KeyboardController | undefined = undefined;
...@@ -91,20 +92,23 @@ ...@@ -91,20 +92,23 @@
constructor() { constructor() {
} }
public onCreate(context: common.ExtensionContext): void public onCreate(context: InputMethodExtensionContext): void
{ {
this.mContext = context; this.mContext = context;
this.initWindow(); // 初始化窗口 this.initWindow(); // 初始化窗口
this.registerListener(); // 注册对输入法框架的事件监听 this.registerListener(); // 注册对输入法框架的事件监听
} }
public onDestroy(): void // 应用生命周期销毁 public onDestroy(): void // 应用生命周期销毁
{ {
this.unRegisterListener(); // 去注册事件监听 this.unRegisterListener(); // 去注册事件监听
if(this.panel) { // 销毁窗口 if(this.panel) { // 销毁窗口
this.panel.hide(); this.panel.hide();
inputMethodAbility.destroyPanel(this.panel); inputMethodAbility.destroyPanel(this.panel);
} }
if(this.mContext) {
this.mContext.destroy();
}
} }
public insertText(text: string): void { public insertText(text: string): void {
...@@ -119,7 +123,7 @@ ...@@ -119,7 +123,7 @@
} }
} }
private initWindow(): void // 初始化窗口 private initWindow(): void // 初始化窗口
{ {
if(this.mContext === undefined) { if(this.mContext === undefined) {
return; return;
...@@ -146,18 +150,18 @@ ...@@ -146,18 +150,18 @@
private registerListener(): void private registerListener(): void
{ {
this.registerInputListener(); // 注册对输入法框架服务的监听 this.registerInputListener(); // 注册对输入法框架服务的监听
... ...
// 注册隐藏键盘事件监听等 // 注册隐藏键盘事件监听等
} }
private registerInputListener(): void { // 注册对输入法框架服务的开启及停止事件监听 private registerInputListener(): void { // 注册对输入法框架服务的开启及停止事件监听
inputMethodAbility.on('inputStart', (kbController, textInputClient) => { inputMethodAbility.on('inputStart', (kbController, textInputClient) => {
this.textInputClient = textInputClient; // 此为输入法客户端实例,由此调用输入法框架提供给输入法应用的功能接口 this.textInputClient = textInputClient; // 此为输入法客户端实例,由此调用输入法框架提供给输入法应用的功能接口
this.boardController = kbController; this.keyboardController = kbController;
}) })
inputMethodAbility.on('inputStop', () => { inputMethodAbility.on('inputStop', () => {
this.onDestroy(); // 销毁KeyboardController this.onDestroy(); // 销毁KeyboardController
}); });
} }
...@@ -166,18 +170,6 @@ ...@@ -166,18 +170,6 @@
inputMethodAbility.off('inputStart'); inputMethodAbility.off('inputStart');
inputMethodAbility.off('inputStop', () => {}); inputMethodAbility.off('inputStop', () => {});
} }
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(); const keyboardController = new KeyboardController();
...@@ -240,7 +232,7 @@ ...@@ -240,7 +232,7 @@
@Component @Component
struct keyItem { struct keyItem {
private keyValue: sourceListType private keyValue: sourceListType = numberSourceListData[0];
@State keyBgc: string = "#fff" @State keyBgc: string = "#fff"
@State keyFontColor: string = "#000" @State keyFontColor: string = "#000"
...@@ -291,9 +283,9 @@ ...@@ -291,9 +283,9 @@
build() { build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceEvenly }) { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceEvenly }) {
Flex({ justifyContent: FlexAlign.SpaceBetween }) { Flex({ justifyContent: FlexAlign.SpaceBetween }) {
ForEach(this.numberList, (item: sourceListType) => { // 数字键盘第一行 ForEach(this.numberList, (item: sourceListType) => { // 数字键盘第一行
keyItem({ keyValue: item }) keyItem({ keyValue: item })
}, (item: sourceListType): sourceListType => item.content); }, (item: sourceListType) => item.content);
} }
.padding({ top: "2%" }) .padding({ top: "2%" })
.width("96%") .width("96%")
...@@ -339,7 +331,7 @@ ...@@ -339,7 +331,7 @@
} }
} }
``` ```
5. 在工程Module对应的[module.json5配置文件](../quick-start/module-configuration-file.md)中注册InputMethodExtensionAbility,type标签需要设置为“inputMethod”,srcEntry标签表示当前InputMethodExtensionAbility组件所对应的代码路径。 5. 在工程Module对应的[module.json5配置文件](../quick-start/module-configuration-file.md)中注册InputMethodExtensionAbility,type标签需要设置为“inputMethod”,srcEntry标签表示当前InputMethodExtensionAbility组件所对应的代码路径。
```json ```json
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
## 导入模块 ## 导入模块
```js ```ts
import InputMethodExtensionAbility from '@ohos.InputMethodExtensionAbility'; import InputMethodExtensionAbility from '@ohos.InputMethodExtensionAbility';
``` ```
...@@ -36,9 +36,11 @@ Extension生命周期回调,在拉起Extension输入法应用时调用,执 ...@@ -36,9 +36,11 @@ Extension生命周期回调,在拉起Extension输入法应用时调用,执
**示例:** **示例:**
```js ```ts
import InputMethodExtensionAbility from '@ohos.InputMethodExtensionAbility';
import Want from '@ohos.app.ability.Want';
class InputMethodExt extends InputMethodExtensionAbility { class InputMethodExt extends InputMethodExtensionAbility {
onCreate(want) { onCreate(want: Want): void {
console.log('onCreate, want:' + want.abilityName); console.log('onCreate, want:' + want.abilityName);
} }
} }
...@@ -54,9 +56,10 @@ Extension生命周期回调,在销毁输入法应用时回调,执行资源 ...@@ -54,9 +56,10 @@ Extension生命周期回调,在销毁输入法应用时回调,执行资源
**示例:** **示例:**
```js ```ts
import InputMethodExtensionAbility from '@ohos.InputMethodExtensionAbility';
class InputMethodExt extends InputMethodExtensionAbility { class InputMethodExt extends InputMethodExtensionAbility {
onDestroy() { onDestroy(): void {
console.log('onDestroy'); console.log('onDestroy');
} }
} }
......
...@@ -8,7 +8,7 @@ InputMethodExtensionContext模块是InputMethodExtensionAbility的上下文环 ...@@ -8,7 +8,7 @@ InputMethodExtensionContext模块是InputMethodExtensionAbility的上下文环
## 导入模块 ## 导入模块
``` ```ts
import InputMethodExtensionContext from '@ohos.InputMethodExtensionContext'; import InputMethodExtensionContext from '@ohos.InputMethodExtensionContext';
``` ```
...@@ -16,10 +16,11 @@ import InputMethodExtensionContext from '@ohos.InputMethodExtensionContext'; ...@@ -16,10 +16,11 @@ import InputMethodExtensionContext from '@ohos.InputMethodExtensionContext';
在使用InputMethodExtensionContext的功能前,需要通过InputMethodExtensionAbility子类实例获取。 在使用InputMethodExtensionContext的功能前,需要通过InputMethodExtensionAbility子类实例获取。
```js ```ts
import InputMethodExtensionAbility from '@ohos.InputMethodExtensionAbility'; import InputMethodExtensionAbility from '@ohos.InputMethodExtensionAbility';
class EntryAbility extends InputMethodExtensionAbility { import Want from '@ohos.app.ability.Want';
onCreate() { class InputMethodExtnAbility extends InputMethodExtensionAbility {
onCreate(want: Want): void {
let context = this.context; let context = this.context;
} }
} }
...@@ -41,8 +42,12 @@ destroy(callback: AsyncCallback\<void>): void ...@@ -41,8 +42,12 @@ destroy(callback: AsyncCallback\<void>): void
**示例:** **示例:**
```js ```ts
this.context.destroy(() => { this.context.destroy((err: Error) => {
if(err) {
console.log('Failed to destroy context.');
return;
}
console.log('Succeeded in destroying context.'); console.log('Succeeded in destroying context.');
}); });
``` ```
...@@ -63,8 +68,10 @@ destroy(): Promise\<void>; ...@@ -63,8 +68,10 @@ destroy(): Promise\<void>;
**示例:** **示例:**
```js ```ts
this.context.destroy().then(() => { this.context.destroy().then(() => {
console.log('Succeed in destroying context.'); console.log('Succeed in destroying context.');
}).catch((err: Error)=>{
console.log('Failed to destroy context.');
}); });
``` ```
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
## 导入模块 ## 导入模块
``` ```ts
import InputMethodSubtype from '@ohos.InputMethodSubtype'; import InputMethodSubtype from '@ohos.InputMethodSubtype';
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册