안드로이드 개발

안드로이드 키패드 숨기는 방법. InputMethodManager

피커 2023. 5. 11. 21:46
728x90
반응형

 

안드로이드 앱을 개발하다보면 입력란에 글자를 모두 입력하고 키패드를 숨기고 싶을때가 있습니다.

이럴때는 아주 간단히 API를 사용해서 숨기기가 가능합니다.

 

1. 키패드가 나오는 화면

   아래 그림처럼 글자를 입력하려고하면 발생하는 입력기를 코드에서 숨기는 방법을 공유합니다.

 

2. 해결 코드

  -. 해결 코드는 아주 간단합니다.

      아래 코드를 복사해서 붙인 다음 hide()함수를 호출해주는 순간 키패드는 숨게 됩니다.

void hide() {
    InputMethodManager imm = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
    //Find the currently focused view, so we can grab the correct window token from it.
    View view = this.getCurrentFocus();
    //If no view currently has focus, create a new one, just so we can grab a window token from it
    if (view == null) {
        view = new View(this);
    }
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

 

3. 관련 백서

Central system API to the overall input method framework (IMF) architecture, which arbitrates interaction between applications and the current input method.

Topics covered here:

  1. Architecture Overview
  2. Applications
  3. Input Methods
  4. Security

Architecture Overview

There are three primary parties involved in the input method framework (IMF) architecture:

  • The input method manager as expressed by this class is the central point of the system that manages interaction between all other parts. It is expressed as the client-side API here which exists in each application context and communicates with a global system service that manages the interaction across all processes.
  • An input method (IME) implements a particular interaction model allowing the user to generate text. The system binds to the current input method that is in use, causing it to be created and run, and tells it when to hide and show its UI. Only one IME is running at a time.
  • Multiple client applications arbitrate with the input method manager for input focus and control over the state of the IME. Only one such client is ever active (working with the IME) at a time.

Applications

In most cases, applications that are using the standard TextView or its subclasses will have little they need to do to work well with soft input methods. The main things you need to be aware of are:

  • Properly set the R.attr.inputType in your editable text views, so that the input method will have enough context to help the user in entering text into them.
  • Deal well with losing screen space when the input method is displayed. Ideally an application should handle its window being resized smaller, but it can rely on the system performing panning of the window if needed. You should set the R.attr.windowSoftInputMode attribute on your activity or the corresponding values on windows you create to help the system determine whether to pan or resize (it will try to determine this automatically but may get it wrong).
  • You can also control the preferred soft input state (open, closed, etc) for your window using the same R.attr.windowSoftInputMode attribute.

More finer-grained control is available through the APIs here to directly interact with the IMF and its IME -- either showing or hiding the input area, letting the user pick an input method, etc.

For the rare people amongst us writing their own text editors, you will need to implement View.onCreateInputConnection(EditorInfo) to return a new instance of your own InputConnection interface allowing the IME to interact with your editor.

Input Methods

An input method (IME) is implemented as a Service, typically deriving from InputMethodService. It must provide the core InputMethod interface, though this is normally handled by InputMethodService and implementors will only need to deal with the higher-level API there.

See the InputMethodService class for more information on implementing IMEs.

Security

There are a lot of security issues associated with input methods, since they essentially have freedom to completely drive the UI and monitor everything the user enters. The Android input method framework also allows arbitrary third party IMEs, so care must be taken to restrict their selection and interactions.

Here are some key points about the security architecture behind the IMF:

  • Only the system is allowed to directly access an IME's InputMethod interface, via the Manifest.permission.BIND_INPUT_METHOD permission. This is enforced in the system by not binding to an input method service that does not require this permission, so the system can guarantee no other untrusted clients are accessing the current input method outside of its control.
  • There may be many client processes of the IMF, but only one may be active at a time. The inactive clients can not interact with key parts of the IMF through the mechanisms described below.
  • Clients of an input method are only given access to its InputMethodSession interface. One instance of this interface is created for each client, and only calls from the session associated with the active client will be processed by the current IME. This is enforced by AbstractInputMethodService for normal IMEs, but must be explicitly handled by an IME that is customizing the raw InputMethodSession implementation.
  • Only the active client's InputConnection will accept operations. The IMF tells each client process whether it is active, and the framework enforces that in inactive processes calls on to the current InputConnection will be ignored. This ensures that the current IME can only deliver events and text edits to the UI that the user sees as being in focus.
  • An IME can never interact with an InputConnection while the screen is off. This is enforced by making all clients inactive while the screen is off, and prevents bad IMEs from driving the UI when the user can not be aware of its behavior.
  • A client application can ask that the system let the user pick a new IME, but can not programmatically switch to one itself. This avoids malicious applications from switching the user to their own IME, which remains running when the user navigates away to another application. An IME, on the other hand, is allowed to programmatically switch the system to another IME, since it already has full control of user input.
  • The user must explicitly enable a new IME in settings before they can switch to it, to confirm with the system that they know about it and want to make it available for use.

If your app targets Android 11 (API level 30) or higher, the methods in this class each return a filtered result by the rules of package visibility, except for the currently connected IME. Apps having a query for the InputMethod#SERVICE_INTERFACE see all IMEs.


Requires the PackageManager#FEATURE_INPUT_METHODS feature which can be detected using PackageManager.hasSystemFeature(String).

반응형