輸入法框架

2024-01-23 17:20 更新

本模塊主要面向普通前臺應(yīng)用(備忘錄、信息、設(shè)置等系統(tǒng)應(yīng)用與三方應(yīng)用),提供對輸入法的控制、管理能力,包括顯示/隱藏輸入法軟鍵盤、切換輸入法、獲取所有輸入法列表等。

說明

本模塊首批接口從API version 6開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。

導(dǎo)入模塊

  1. import inputMethod from '@ohos.inputMethod';

常量8+

常量值。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù)名

類型

常量值

說明

MAX_TYPE_NUM

number

128

可支持的最大輸入法個數(shù)。

InputMethodProperty8+

輸入法應(yīng)用屬性。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

名稱

類型

可讀

可寫

說明

name9+

string

輸入法內(nèi)部名稱。必填。

id9+

string

輸入法唯一標(biāo)識。必填。

label9+

string

輸入法對外顯示名稱。 非必填。

icon9+

string

輸入法圖標(biāo)數(shù)據(jù)。非必填。

iconId9+

number

輸入法圖標(biāo)資源號。非必填。

extra9+

object

輸入法擴(kuò)展信息。 必填。

packageName(deprecated)

string

輸入法包名。必填。

說明: 從API version 8開始支持,從API version 9開始廢棄,建議使用name替代。

methodId(deprecated)

string

輸入法唯一標(biāo)識。必填。

說明: 從API version 8開始支持,從API version 9開始廢棄,建議使用id替代。

inputMethod.getController9+

getController(): InputMethodController

獲取客戶端實例InputMethodController。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

InputMethodController

回調(diào)返回當(dāng)前客戶端實例。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800006

input method controller error.

示例:

  1. let inputMethodController = inputMethod.getController();

inputMethod.getSetting9+

getSetting(): InputMethodSetting

獲取客戶端設(shè)置實例InputMethodSetting。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

InputMethodSetting

回調(diào)返回當(dāng)前客戶端設(shè)置實例。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼

錯誤碼ID

錯誤信息

12800007

input method settings extension error.

示例:

  1. let inputMethodSetting = inputMethod.getSetting();

inputMethod.switchInputMethod9+

switchInputMethod(target: InputMethodProperty, callback: AsyncCallback<boolean>): void

切換輸入法。使用callback異步回調(diào)。

需要權(quán)限: ohos.permission.CONNECT_IME_ABILITY,僅系統(tǒng)應(yīng)用可用。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

target

InputMethodProperty

傳入要切換的目標(biāo)輸入法。

callback

AsyncCallback<boolean>

回調(diào)函數(shù)。當(dāng)輸入法切換成功,err為undefined,data為true;否則為錯誤對象。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800005

configuration persisting error.

12800008

input method manager service error.

示例:

  1. let im = inputMethod.getCurrentInputMethod();
  2. let prop = {
  3. packageName: im.packageName,
  4. methodId: im.methodId,
  5. name: im.packageName,
  6. id: im.methodId,
  7. extra: {}
  8. }
  9. try{
  10. inputMethod.switchInputMethod(prop, (err, result) => {
  11. if (err !== undefined) {
  12. console.error('Failed to switchInputMethod: ' + JSON.stringify(err));
  13. return;
  14. }
  15. if (result) {
  16. console.info('Succeeded in switching inputmethod.');
  17. } else {
  18. console.error('Failed to switchInputMethod.');
  19. }
  20. });
  21. } catch(err) {
  22. console.error('Failed to switchInputMethod: ' + JSON.stringify(err));
  23. }

inputMethod.switchInputMethod9+

switchInputMethod(target: InputMethodProperty): Promise<boolean>

切換輸入法。使用promise異步回調(diào)。

需要權(quán)限: ohos.permission.CONNECT_IME_ABILITY,僅系統(tǒng)應(yīng)用可用。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

target

InputMethodProperty

傳入要切換的目標(biāo)輸入法。

返回值:

類型

說明

Promise<boolean>

Promise對象。返回true表示切換輸入法成功;返回false表示切換輸入法失敗。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800005

configuration persisting error.

12800008

input method manager service error.

示例:

  1. let im = inputMethod.getCurrentInputMethod();
  2. let prop = {
  3. packageName: im.packageName,
  4. methodId: im.methodId,
  5. name: im.packageName,
  6. id: im.methodId,
  7. extra: {}
  8. }
  9. try {
  10. inputMethod.switchInputMethod(prop).then((result) => {
  11. if (result) {
  12. console.info('Succeeded in switching inputmethod.');
  13. } else {
  14. console.error('Failed to switchInputMethod.');
  15. }
  16. }).catch((err) => {
  17. console.error('Failed to switchInputMethod: ' + JSON.stringify(err));
  18. })
  19. } catch(err) {
  20. console.error('Failed to switchInputMethod: ' + JSON.stringify(err));
  21. }

inputMethod.getCurrentInputMethod9+

getCurrentInputMethod(): InputMethodProperty

獲取當(dāng)前輸入法擴(kuò)展應(yīng)用,提供同步接口,返回當(dāng)前輸入法屬性。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

InputMethodProperty

返回當(dāng)前輸入法屬性對象。

示例:

  1. let currentIme = inputMethod.getCurrentInputMethod();

inputMethod.switchCurrentInputMethodSubtype9+

switchCurrentInputMethodSubtype(target: InputMethodSubtype, callback: AsyncCallback<boolean>): void

在當(dāng)前輸入法應(yīng)用內(nèi)切換子類型。使用callback異步回調(diào)。

需要權(quán)限: ohos.permission.CONNECT_IME_ABILITY,僅系統(tǒng)應(yīng)用可用。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

target

InputMethodSubtype

傳入要切換的目標(biāo)輸入法子類型。

callback

AsyncCallback<boolean>

回調(diào)函數(shù)。當(dāng)輸入法子類型切換成功,err為undefined,data為true;否則為錯誤對象。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800005

configuration persisting error.

12800008

input method manager service error.

示例:

  1. try {
  2. inputMethod.switchCurrentInputMethodSubtype({
  3. id: "ServiceExtAbility",
  4. label: "",
  5. name: "com.example.kikakeyboard",
  6. mode: "upper",
  7. locale: "",
  8. language: "",
  9. icon: "",
  10. iconId: 0,
  11. extra: {}
  12. }, (err, result) => {
  13. if (err !== undefined) {
  14. console.error('Failed to switchCurrentInputMethodSubtype: ' + JSON.stringify(err));
  15. return;
  16. }
  17. if (result) {
  18. console.info('Succeeded in switching currentInputMethodSubtype.');
  19. } else {
  20. console.error('Failed to switchCurrentInputMethodSubtype');
  21. }
  22. });
  23. } catch(err) {
  24. console.error('Failed to switchCurrentInputMethodSubtype: ' + JSON.stringify(err));
  25. }

inputMethod.switchCurrentInputMethodSubtype9+

switchCurrentInputMethodSubtype(target: InputMethodSubtype): Promise<boolean>

在當(dāng)前輸入法應(yīng)用內(nèi)切換子類型。使用promise異步回調(diào)。

需要權(quán)限: ohos.permission.CONNECT_IME_ABILITY,僅系統(tǒng)應(yīng)用可用。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

target

InputMethodSubtype

傳入要切換的目標(biāo)輸入法子類型。

返回值:

類型

說明

Promise<boolean>

Promise對象。返回true表示在當(dāng)前輸入法應(yīng)用內(nèi)切換子類型成功;返回false表示在當(dāng)前輸入法應(yīng)用內(nèi)切換子類型失敗。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800005

configuration persisting error.

12800008

input method manager service error.

示例:

  1. try {
  2. inputMethod.switchCurrentInputMethodSubtype({
  3. id: "ServiceExtAbility",
  4. label: "",
  5. name: "com.example.kikakeyboard",
  6. mode: "upper",
  7. locale: "",
  8. language: "",
  9. icon: "",
  10. iconId: 0,
  11. extra: {}
  12. }).then((result) => {
  13. if (result) {
  14. console.info('Succeeded in switching currentInputMethodSubtype.');
  15. } else {
  16. console.error('Failed to switchCurrentInputMethodSubtype.');
  17. }
  18. }).catch((err) => {
  19. console.error('Failed to switchCurrentInputMethodSubtype: ' + JSON.stringify(err));
  20. })
  21. } catch(err) {
  22. console.error('Failed to switchCurrentInputMethodSubtype: ' + JSON.stringify(err));
  23. }

inputMethod.getCurrentInputMethodSubtype9+

getCurrentInputMethodSubtype(): InputMethodSubtype

獲取當(dāng)前輸入法子類型。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

InputMethodSubtype

返回當(dāng)前輸入法子類型對象。

示例:

  1. let currentImeSubType = inputMethod.getCurrentInputMethodSubtype();

inputMethod.switchCurrentInputMethodAndSubtype9+

switchCurrentInputMethodAndSubtype(inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype, callback: AsyncCallback<boolean>): void

切換至指定輸入法應(yīng)用的指定子類型,用于跨輸入法應(yīng)用切換子類型。使用callback異步回調(diào)。

需要權(quán)限: ohos.permission.CONNECT_IME_ABILITY,僅系統(tǒng)應(yīng)用可用。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

inputMethodProperty

InputMethodProperty

傳入要切換的目標(biāo)輸入法。

inputMethodSubtype

InputMethodSubtype

傳入要切換的目標(biāo)輸入法子類型。

callback

AsyncCallback<boolean>

回調(diào)函數(shù)。當(dāng)輸入法和子類型切換成功,err為undefined,data為獲取到的切換子類型結(jié)果true;否則為錯誤對象。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800005

configuration persisting error.

12800008

input method manager service error.

示例:

  1. let im = inputMethod.getCurrentInputMethod();
  2. let imSubType = inputMethod.getCurrentInputMethodSubtype();
  3. try {
  4. inputMethod.switchCurrentInputMethodAndSubtype(im, imSubType, (err,result) => {
  5. if (err !== undefined) {
  6. console.error('Failed to switchCurrentInputMethodAndSubtype: ' + JSON.stringify(err));
  7. return;
  8. }
  9. if (result) {
  10. console.info('Succeeded in switching currentInputMethodAndSubtype.');
  11. } else {
  12. console.error('Failed to switchCurrentInputMethodAndSubtype.');
  13. }
  14. });
  15. } catch (err) {
  16. console.error('Failed to switchCurrentInputMethodAndSubtype: ' + JSON.stringify(err));
  17. }

inputMethod.switchCurrentInputMethodAndSubtype9+

switchCurrentInputMethodAndSubtype(inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype): Promise<boolean>

切換至指定輸入法應(yīng)用的指定子類型,用于跨輸入法應(yīng)用切換子類型。使用promise異步回調(diào)。

需要權(quán)限: ohos.permission.CONNECT_IME_ABILITY,僅系統(tǒng)應(yīng)用可用。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

inputMethodProperty

InputMethodProperty

傳入要切換的目標(biāo)輸入法。

inputMethodSubtype

InputMethodSubtype

傳入要切換的目標(biāo)輸入法子類型。

返回值:

類型

說明

Promise<boolean>

Promise對象。返回true表示切換至指定輸入法應(yīng)用的指定子類型成功;返回false表示切換至指定輸入法應(yīng)用的指定子類型失敗。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800005

configuration persisting error.

12800008

input method manager service error.

示例:

  1. let im = inputMethod.getCurrentInputMethod();
  2. let imSubType = inputMethod.getCurrentInputMethodSubtype();
  3. try {
  4. inputMethod.switchCurrentInputMethodAndSubtype(im, imSubType).then((result) => {
  5. if (result) {
  6. console.info('Succeeded in switching currentInputMethodAndSubtype.');
  7. } else {
  8. console.error('Failed to switchCurrentInputMethodAndSubtype.');
  9. }
  10. }).catch((err) => {
  11. console.error('Failed to switchCurrentInputMethodAndSubtype: ' + JSON.stringify(err));
  12. })
  13. } catch(err) {
  14. console.error('Failed to switchCurrentInputMethodAndSubtype: ' + JSON.stringify(err));
  15. }

inputMethod.getInputMethodController(deprecated)

getInputMethodController(): InputMethodController

獲取客戶端實例InputMethodController

說明

從API version 6開始支持,從API version 9開始廢棄, 建議使用getController()替代。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

InputMethodController

回調(diào)返回當(dāng)前客戶端實例。

示例:

  1. let inputMethodController = inputMethod.getInputMethodController();

inputMethod.getInputMethodSetting(deprecated)

getInputMethodSetting(): InputMethodSetting

獲取客戶端設(shè)置實例InputMethodSetting。

說明

從API version 6開始支持,從API version 9開始廢棄, 建議使用getSetting()替代。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

InputMethodSetting

回調(diào)返回當(dāng)前客戶端設(shè)置實例。

示例:

  1. let inputMethodSetting = inputMethod.getInputMethodSetting();

InputMethodController

下列API示例中都需使用getController獲取到InputMethodController實例,再通過此實例調(diào)用對應(yīng)方法。

stopInputSession9+

stopInputSession(callback: AsyncCallback<boolean>): void

結(jié)束輸入會話。通過點擊輸入框?qū)崿F(xiàn)輸入會話的開啟之后該接口的調(diào)用才可生效。使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<boolean>

回調(diào)函數(shù)。當(dāng)結(jié)束輸入會話成功時,err為undefined,data為true;否則為錯誤對象。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800003

input method client error.

12800008

input method manager service error.

示例:

  1. try {
  2. inputMethodController.stopInputSession((error, result) => {
  3. if (error !== undefined) {
  4. console.error('Failed to stopInputSession: ' + JSON.stringify(error));
  5. return;
  6. }
  7. if (result) {
  8. console.info('Succeeded in stopping inputSession.');
  9. } else {
  10. console.error('Failed to stopInputSession.');
  11. }
  12. });
  13. } catch(error) {
  14. console.error('Failed to stopInputSession: ' + JSON.stringify(error));
  15. }

stopInputSession9+

stopInputSession(): Promise<boolean>

結(jié)束輸入會話。通過點擊輸入框?qū)崿F(xiàn)輸入會話的開啟之后此接口才可生效。使用promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

Promise<boolean>

Promise對象。返回true表示結(jié)束輸入會話成功;返回false表示結(jié)束輸入會話失敗。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼

錯誤碼ID

錯誤信息

12800003

input method client error.

12800008

input method manager service error.

示例:

  1. try {
  2. inputMethodController.stopInputSession().then((result) => {
  3. if (result) {
  4. console.info('Succeeded in stopping inputSession.');
  5. } else {
  6. console.error('Failed to stopInputSession.');
  7. }
  8. }).catch((err) => {
  9. console.error('Failed to stopInputSession: ' + JSON.stringify(err));
  10. })
  11. } catch(err) {
  12. console.error('Failed to stopInputSession: ' + JSON.stringify(err));
  13. }

showSoftKeyboard9+

showSoftKeyboard(callback: AsyncCallback<void>): void

顯示軟鍵盤。需要與輸入框綁定使用。當(dāng)點擊輸入框后,才可通過該接口的調(diào)用顯示出當(dāng)前輸入法的軟鍵盤。使用callback異步回調(diào)。

需要權(quán)限: ohos.permission.CONNECT_IME_ABILITY,僅系統(tǒng)應(yīng)用可用。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)軟鍵盤顯示成功。err為undefined,否則為錯誤對象。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800003

input method client error.

12800008

input method manager service error.

示例:

  1. inputMethodController.showSoftKeyboard((err) => {
  2. if (err === undefined) {
  3. console.info('Succeeded in showing softKeyboard.');
  4. } else {
  5. console.error('Failed to showSoftKeyboard: ' + JSON.stringify(err));
  6. }
  7. })

showSoftKeyboard9+

showSoftKeyboard(): Promise<void>

顯示軟鍵盤。需要與輸入框綁定使用。當(dāng)點擊輸入框后,才可通過該接口的調(diào)用顯示出當(dāng)前輸入法的軟鍵盤。使用Promise異步回調(diào)。

需要權(quán)限: ohos.permission.CONNECT_IME_ABILITY,僅系統(tǒng)應(yīng)用可用。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

Promise<void>

無返回結(jié)果的Promise對象。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800003

input method client error.

12800008

input method manager service error.

示例:

  1. inputMethodController.showSoftKeyboard().then(() => {
  2. console.log('Succeeded in showing softKeyboard.');
  3. }).catch((err) => {
  4. console.error('Failed to showSoftKeyboard: ' + JSON.stringify(err));
  5. });

hideSoftKeyboard9+

hideSoftKeyboard(callback: AsyncCallback<void>): void

隱藏軟鍵盤。需要與輸入框綁定使用。點擊輸入框后,才可以使用該接口的調(diào)用隱藏軟鍵盤。使用callback異步回調(diào)。

需要權(quán)限: ohos.permission.CONNECT_IME_ABILITY,僅系統(tǒng)應(yīng)用可用。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)軟鍵盤隱藏成功。err為undefined,否則為錯誤對象。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800003

input method client error.

12800008

input method manager service error.

示例:

  1. inputMethodController.hideSoftKeyboard((err) => {
  2. if (err === undefined) {
  3. console.info('Succeeded in hiding softKeyboard.');
  4. } else {
  5. console.error('Failed to hideSoftKeyboard: ' + JSON.stringify(err));
  6. }
  7. })

hideSoftKeyboard9+

hideSoftKeyboard(): Promise<void>

隱藏軟鍵盤。需要與輸入框綁定使用。點擊輸入框后,才可以使用該接口的調(diào)用隱藏軟鍵盤。使用Promise異步回調(diào)。

需要權(quán)限: ohos.permission.CONNECT_IME_ABILITY,僅系統(tǒng)應(yīng)用可用。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

Promise<void>

無返回結(jié)果的Promise對象。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800003

input method client error.

12800008

input method manager service error.

示例:

  1. inputMethodController.hideSoftKeyboard().then(() => {
  2. console.log('Succeeded in hiding softKeyboard.');
  3. }).catch((err) => {
  4. console.error('Failed to hideSoftKeyboard: ' + JSON.stringify(err));
  5. });

stopInput(deprecated)

stopInput(callback: AsyncCallback<boolean>): void

結(jié)束輸入會話。通過點擊輸入框?qū)崿F(xiàn)輸入會話的開啟之后該接口的調(diào)用才可生效。使用callback異步回調(diào)。

說明

從API version 6開始支持,從API version 9開始廢棄, 建議使用stopInputSession()替代。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<boolean>

回調(diào)函數(shù)。當(dāng)輸入法隱藏成功,err為undefined,data為true;否則為錯誤對象。

示例:

  1. inputMethodController.stopInput((error, result) => {
  2. if (error !== undefined) {
  3. console.error('Failed to stopInput: ' + JSON.stringify(error));
  4. return;
  5. }
  6. if (result) {
  7. console.info('Succeeded in stopping input.');
  8. } else {
  9. console.error('Failed to stopInput.');
  10. }
  11. });

stopInput(deprecated)

stopInput(): Promise<boolean>

結(jié)束輸入會話。通過點擊輸入框?qū)崿F(xiàn)輸入會話的開啟之后該接口的調(diào)用才可生效。使用promise異步回調(diào)。

說明

從API version 6開始支持,從API version 9開始廢棄, 建議使用stopInputSession()替代。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

Promise<boolean>

Promise對象。返回true表示輸入法隱藏成功;返回false表示輸入法隱藏失敗。

示例:

  1. inputMethodController.stopInput().then((result) => {
  2. if (result) {
  3. console.info('Succeeded in stopping input.');
  4. } else {
  5. console.error('Failed to stopInput.');
  6. }
  7. }).catch((err) => {
  8. console.error('Failed to stopInput: ' + err);
  9. })

InputMethodSetting8+

下列API示例中都需使用getSetting獲取到InputMethodSetting實例,再通過此實例調(diào)用對應(yīng)方法。

on('imeChange')9+

on(type: 'imeChange', callback: (inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => void): void

訂閱輸入法及子類型變化監(jiān)聽事件。使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

type

string

設(shè)置監(jiān)聽類型。

-type為‘imeChange’時表示訂閱輸入法及子類型變化監(jiān)聽事件。

callback

(inputMethodProperty: InputMethodProperty, inputMethodSubtype:

InputMethodSubtype) => void

回調(diào)函數(shù),返回輸入法屬性對象及輸入法子類型對象。

示例:

  1. inputMethodSetting.on('imeChange', (inputMethodProperty, inputMethodSubtype) => {
  2. console.info('Succeeded in subscribing imeChange: inputMethodProperty: ' + JSON.stringify(inputMethodProperty) + " , inputMethodSubtype: " + JSON.stringify(inputMethodSubtype));
  3. });

off('imeChange')9+

off(type: 'imeChange', callback?: (inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => void): void

取消訂閱輸入法及子類型變化監(jiān)聽事件。使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

type

string

設(shè)置監(jiān)聽類型。

-type為‘imeChange’時表示取消訂閱輸入法及子類型變化監(jiān)聽事件。

callback

(inputMethodProperty: InputMethodProperty, inputMethodSubtype:

InputMethodSubtype) => void

回調(diào)函數(shù),返回取消訂閱的輸入法屬性對象及輸入法子類型對象。

示例:

  1. inputMethodSetting.off('imeChange');

listInputMethodSubtype9+

listInputMethodSubtype(inputMethodProperty: InputMethodProperty, callback: AsyncCallback<Array<InputMethodSubtype>>): void

獲取指定輸入法應(yīng)用的所有子類型。使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

inputMethodProperty

InputMethodProperty

子類型所屬的輸入法應(yīng)用。

callback

AsyncCallback<Array<InputMethodSubtype>>

回調(diào)函數(shù),返回指定輸入法應(yīng)用的所有子類型。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800001

package manager error.

12800008

input method manager service error.

示例:

  1. let inputMethodProperty = {
  2. packageName: 'com.example.kikakeyboard',
  3. methodId: 'com.example.kikakeyboard',
  4. name: 'com.example.kikakeyboard',
  5. id: 'com.example.kikakeyboard',
  6. extra:{}
  7. }
  8. try {
  9. inputMethodSetting.listInputMethodSubtype(inputMethodProperty, (err,data) => {
  10. if (err !== undefined) {
  11. console.error('Failed to listInputMethodSubtype: ' + JSON.stringify(err));
  12. return;
  13. }
  14. console.log('Succeeded in listing inputMethodSubtype.');
  15. });
  16. } catch (err) {
  17. console.error('Failed to listInputMethodSubtype: ' + JSON.stringify(err));
  18. }

listInputMethodSubtype9+

listInputMethodSubtype(inputMethodProperty: InputMethodProperty): Promise<Array<InputMethodSubtype>>

獲取指定輸入法應(yīng)用的所有子類型。使用promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

inputMethodProperty

InputMethodProperty

子類型所屬的輸入法應(yīng)用。

返回值:

類型

說明

Promise<Array<InputMethodSubtype>>

Promise對象,返回指定輸入法應(yīng)用的所有子類型。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼

錯誤碼ID

錯誤信息

12800001

package manager error.

12800008

input method manager service error.

示例:

  1. let inputMethodProperty = {
  2. packageName: 'com.example.kikakeyboard',
  3. methodId: 'com.example.kikakeyboard',
  4. name: 'com.example.kikakeyboard',
  5. id: 'com.example.kikakeyboard',
  6. extra:{}
  7. }
  8. try {
  9. inputMethodSetting.listInputMethodSubtype(inputMethodProperty).then((data) => {
  10. console.info('Succeeded in listing inputMethodSubtype.');
  11. }).catch((err) => {
  12. console.error('Failed to listInputMethodSubtype: ' + JSON.stringify(err));
  13. })
  14. } catch(err) {
  15. console.error('Failed to listInputMethodSubtype: ' + JSON.stringify(err));
  16. }

listCurrentInputMethodSubtype9+

listCurrentInputMethodSubtype(callback: AsyncCallback<Array<InputMethodSubtype>>): void

查詢當(dāng)前輸入法應(yīng)用的所有子類型。使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<Array<InputMethodSubtype>>

回調(diào)函數(shù),返回當(dāng)前輸入法應(yīng)用的所有子類型。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼

錯誤碼ID

錯誤信息

12800001

package manager error.

12800008

input method manager service error.

示例:

  1. try {
  2. inputMethodSetting.listCurrentInputMethodSubtype((err, data) => {
  3. if (err !== undefined) {
  4. console.error('Failed to listCurrentInputMethodSubtype: ' + JSON.stringify(err));
  5. return;
  6. }
  7. console.log('Succeeded in listing currentInputMethodSubtype.');
  8. });
  9. } catch(err) {
  10. console.error('Failed to listCurrentInputMethodSubtype: ' + JSON.stringify(err));
  11. }

listCurrentInputMethodSubtype9+

listCurrentInputMethodSubtype(): Promise<Array<InputMethodSubtype>>

查詢當(dāng)前輸入法的子類型列表。使用promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

Promise<Array<InputMethodSubtype>>

Promise對象,返回當(dāng)前輸入法應(yīng)用的所有子類型。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800001

package manager error.

12800008

input method manager service error.

示例:

  1. try {
  2. inputMethodSetting.listCurrentInputMethodSubtype().then((data) => {
  3. console.info('Succeeded in listing currentInputMethodSubtype.');
  4. }).catch((err) => {
  5. console.error('Failed to listCurrentInputMethodSubtype: ' + JSON.stringify(err));
  6. })
  7. } catch(err) {
  8. console.error('Failed to listCurrentInputMethodSubtype: ' + JSON.stringify(err));
  9. }

getInputMethods9+

getInputMethods(enable: boolean, callback: AsyncCallback<Array<InputMethodProperty>>): void

獲取已激活/未激活輸入法列表。參數(shù)enable取true,返回已激活輸入法列表,取false返回未激活輸入法列表。已激活/未激活輸入法的確切功能當(dāng)前版本未支持。當(dāng)前版本中,已激活輸入法包括當(dāng)前使用的輸入法,未激活輸入法包括當(dāng)前輸入法以外的其他已安裝的輸入法。使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

enable

boolean

指定返回已激活/未激活。

callback

AsyncCallback<Array<InputMethodProperty>>

回調(diào)函數(shù),返回已激活/未激活輸入法列表。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800001

package manager error.

12800008

input method manager service error.

示例:

  1. try {
  2. inputMethodSetting.getInputMethods(true, (err,data) => {
  3. if (err !== undefined) {
  4. console.error('Failed to getInputMethods: ' + JSON.stringify(err));
  5. return;
  6. }
  7. console.log('Succeeded in getting inputMethods.');
  8. });
  9. } catch (err) {
  10. console.error('Failed to getInputMethods: ' + JSON.stringify(err));
  11. }

getInputMethods9+

getInputMethods(enable: boolean): Promise<Array<InputMethodProperty>>

獲取已激活/未激活輸入法列表。參數(shù)enable取true,返回已激活輸入法列表,取false返回未激活輸入法列表。已激活/未激活輸入法的確切功能當(dāng)前版本未支持。當(dāng)前版本中,已激活輸入法包括當(dāng)前使用的輸入法,未激活輸入法包括當(dāng)前輸入法以外的其他已安裝的輸入法。使用promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

enable

boolean

指定返回已激活/未激活。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800001

package manager error.

12800008

input method manager service error.

返回值:

類型

說明

Promise<Array<InputMethodProperty>>

Promise對象,返回已激活/未激活輸入法列表。

示例:

  1. try {
  2. inputMethodSetting.getInputMethods(true).then((data) => {
  3. console.info('Succeeded in getting inputMethods.');
  4. }).catch((err) => {
  5. console.error('Failed to getInputMethods: ' + JSON.stringify(err));
  6. })
  7. } catch(err) {
  8. console.error('Failed to getInputMethods: ' + JSON.stringify(err));
  9. }

showOptionalInputMethods9+

showOptionalInputMethods(callback: AsyncCallback<boolean>): void

顯示輸入法選擇對話框。使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<boolean>

回調(diào)函數(shù)。當(dāng)輸入法選擇對話框顯示成功,err為undefined,data為true;否則為錯誤對象。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800008

input method manager service error.

示例:

  1. try {
  2. inputMethodSetting.showOptionalInputMethods((err, data) => {
  3. if (err !== undefined) {
  4. console.error('Failed to showOptionalInputMethods: ' + JSON.stringify(err));
  5. return;
  6. }
  7. console.info('Succeeded in showing optionalInputMethods.');
  8. });
  9. } catch (err) {
  10. console.error('Failed to showOptionalInputMethods: ' + JSON.stringify(err));
  11. }

showOptionalInputMethods9+

showOptionalInputMethods(): Promise<boolean>

顯示輸入法選擇對話框。使用promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

Promise<boolean>

Promise對象。返回true表示輸入法選擇對話框顯示成功;返回false表示輸入法選擇對話框顯示失敗。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見輸入法框架錯誤碼。

錯誤碼ID

錯誤信息

12800008

input method manager service error.

示例:

  1. inputMethodSetting.showOptionalInputMethods().then((data) => {
  2. console.info('Succeeded in showing optionalInputMethods.');
  3. }).catch((err) => {
  4. console.error('Failed to showOptionalInputMethods: ' + JSON.stringify(err));
  5. })

listInputMethod(deprecated)

listInputMethod(callback: AsyncCallback<Array<InputMethodProperty>>): void

查詢已安裝的輸入法列表。使用callback異步回調(diào)。

說明

從API version 8開始支持,從API version 9開始廢棄, 建議使用getInputMethods替代。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<Array<InputMethodProperty>>

回調(diào)函數(shù),返回已安裝的輸入法列表。

示例:

  1. inputMethodSetting.listInputMethod((err,data) => {
  2. if (err !== undefined) {
  3. console.error('Failed to listInputMethod: ' + JSON.stringify(err));
  4. return;
  5. }
  6. console.log('Succeeded in listing inputMethod.');
  7. });

listInputMethod(deprecated)

listInputMethod(): Promise<Array<InputMethodProperty>>

查詢已安裝的輸入法列表。使用promise異步回調(diào)。

說明

從API version 8開始支持,從API version 9開始廢棄, 建議使用getInputMethods替代。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

Promise<Array<InputMethodProperty>>

Promise對象,返回已安裝輸入法列表。

示例:

  1. inputMethodSetting.listInputMethod().then((data) => {
  2. console.info('Succeeded in listing inputMethod.');
  3. }).catch((err) => {
  4. console.error('Failed to listInputMethod: ' + JSON.stringify(err));
  5. })

displayOptionalInputMethod(deprecated)

displayOptionalInputMethod(callback: AsyncCallback<void>): void

顯示輸入法選擇對話框。使用callback異步回調(diào)。

說明

從API version 8開始支持,從API version 9開始廢棄, 建議使用showOptionalInputMethods()替代。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)輸入法選擇對話框顯示成功。err為undefined,否則為錯誤對象。

示例:

  1. inputMethodSetting.displayOptionalInputMethod((err) => {
  2. if (err !== undefined) {
  3. console.error('Failed to displayOptionalInputMethod: ' + JSON.stringify(err));
  4. return;
  5. }
  6. console.info('Succeeded in displaying optionalInputMethod.');
  7. });

displayOptionalInputMethod(deprecated)

displayOptionalInputMethod(): Promise<void>

顯示輸入法選擇對話框。使用promise異步回調(diào)。

說明

從API version 8開始支持,從API version 9開始廢棄, 建議使用showOptionalInputMethods()替代。

系統(tǒng)能力: SystemCapability.MiscServices.InputMethodFramework

返回值:

類型

說明

Promise<void>

無返回結(jié)果的Promise對象。

示例:

  1. inputMethodSetting.displayOptionalInputMethod().then(() => {
  2. console.info('Succeeded in displaying optionalInputMethod.');
  3. }).catch((err) => {
  4. console.error('Failed to displayOptionalInputMethod: ' + JSON.stringify(err));
  5. })
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號