SoftKeyboard详解

本文详细介绍了输入法组件中关键的方法及其用途,包括初始化、视图创建、输入启动与完成等核心过程。通过这些API,开发者可以更好地定制和扩展输入法功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. public void onCreate()

Main initialization of the input method component. Be sure to call to super class.

Overrides: onCreate() in InputMethodService

2. public void onInitializeInterface()

This is the point where you can do all of your UI initialization. It is called after creation and any configuration change.

Overrides: onInitializeInterface() in InputMethodService

3. public View onCreateInputView()

Called by the framework when your view for creating input needs to be generated. This will be called the first time your input method is displayed, and every time it needs to be re-created such as due to a configuration change.

Overrides: onCreateInputView() in InputMethodService

4. public View onCreateCandidatesView()

Called by the framework when your view for showing candidates needs to be generated, like onCreateInputView.

Overrides: onCreateCandidatesView() in InputMethodService

5. public void onStartInput(EditorInfo attribute, boolean restarting)

This is the main point where we do our initialization of the input method to begin operating on an application. At this point we have been bound to the client, and are now receiving all of the detailed information about the target of our edits.

Overrides: onStartInput(...) in InputMethodService

Parameters:

  • attribute The attributes of the editor that input is starting in.
  • restarting Set to true if input is restarting in the same editor such as because the application has changed the text in the editor. Otherwise will be false, indicating this is a new session with the editor.

6. public void onFinishInput()

This is called when the user is done editing a field. We can use this to reset our state.

Overrides: onFinishInput() in InputMethodService

7. public void onStartInputView(EditorInfo attribute, boolean restarting)

Called when the input view is being shown and input has started on a new editor. This will always be called after onStartInput, allowing you to do your general setup there and just view-specific setup here. You are guaranteed that onCreateInputView() will have been called some time before this function is called.

Overrides: onStartInputView(...) in InputMethodService

Parameters:

  • attribute Description of the type of text being edited.
  • restarting Set to true if we are restarting input on the same text field as before.

8. public void onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd)

Deal with the editor reporting movement of its cursor.

Overrides: onUpdateSelection(...) in InputMethodService

9. public void onDisplayCompletions(CompletionInfo[] completions)

This tells us about completions that the editor has determined based on the current text in it. We want to use this in fullscreen mode to show the completions ourself, since the editor can not be seen in that situation.

Overrides: onDisplayCompletions(...) in InputMethodService

10. private boolean translateKeyDown(int keyCode, KeyEvent event)

This translates incoming hard key events into edit operations on an InputConnection. It is only needed when using the PROCESS_HARD_KEYS option.

11. public boolean onKeyDown(int keyCode, KeyEvent event)

Use this to monitor key events being delivered to the application. We get first crack at them, and can either resume them or let them continue to the app.

Overrides: onKeyDown(...) in InputMethodService

Parameters:

  • keyCode The value in event.getKeyCode().
  • event Description of the key event.

Returns:

  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

12. public boolean onKeyUp(int keyCode, KeyEvent event)

Use this to monitor key events being delivered to the application. We get first crack at them, and can either resume them or let them continue to the app.

Overrides: onKeyUp(...) in InputMethodService

Parameters:

  • keyCode The value in event.getKeyCode().
  • event Description of the key event.

Returns:

  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

13. private void commitTyped(InputConnection inputConnection)

Helper function to commit any text being composed into the editor.

14. private void updateShiftKeyState(EditorInfo attr)

Helper to update the shift state of our keyboard based on the initial editor state.

15. private boolean isAlphabet(int code)

Helper to determine if a given character code is alphabetic(字母的、照字母次序的).

16. private void keyDownUp(int keyEventCode)

Helper to send a key down / key up pair to the current editor.

17. private void sendKey(int keyCode)

Helper to send a character to the editor as raw key events.

18. public void onKey(int primaryCode, int[] keyCodes)

Send a key press to the listener.

Specified by: onKey(...) in OnKeyboardActionListener

Parameters:

  • primaryCode this is the key that was pressed
  • keyCodes the codes for all the possible alternative keys with the primary code being the first. If the primary key code is a single character such as an alphabet or number or symbol, the alternatives will include other characters that may be on the same key or adjacent keys. These codes are useful to correct for accidental presses of a key adjacent to the intended key.

19. public void onText(CharSequence text)

Sends a sequence of characters to the listener.

Specified by: onText(...) in OnKeyboardActionListener

Parameters:

  • text the sequence of characters to be displayed.

20. private void updateCandidates()

Update the list of available candidates from the current composing text. This will need to be filled in by however you are determining candidates.

21. public void setSuggestions(List<String> suggestions, boolean completions, boolean typedWordValid)

22. private void handleBackspace()

23. private void handleShift()

24. private void handleCharacter(int primaryCode, int[] keyCodes)

25. private void handleClose()

26. private void checkToggleCapsLock()

27. private String getWordSeparators()

28. public boolean isWordSeparator(int code)

29. public void pickDefaultCandidate()

30. public void pickSuggestionManually(int index)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值