简体   繁体   English

在EditText视图中输入后隐藏键盘

[英]Hiding the keyboard after typing in EditText view

I came across this piece of code but I am wondering what this code does and what exactly is InputMethodManager and where should I type in this code in my class? 我碰到了这段代码,但是我想知道这段代码的作用以及InputMethodManager的确切含义,我应该在类中的什么地方键入该代码? Will it go in the onCreate() method or should I make a new method? 它会放在onCreate()方法中还是应该创建一个新方法? And again, I want to understand how this method works. 同样,我想了解这种方法的工作原理。

Thanks for your answer in advance :) I appreciate the help 谢谢您的预先答复:)感谢您的帮助

    InputMethodManager inputManager = 
    (InputMethodManager) context.
        getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.hideSoftInputFromWindow(
    this.getCurrentFocus().getWindowToken(),
    InputMethodManager.HIDE_NOT_ALWAYS); 

The javadoc of InputMethod is quite descriptive about it InputMethodJavadoc对此具有描述性

Central system API to the overall input method framework (IMF) architecture, which arbitrates interaction between applications and the current input method. 总体输入法框架(IMF)架构的中央系统API,它可仲裁应用程序与当前输入法之间的交互。 You can retrieve an instance of this interface with Context.getSystemService(). 您可以使用Context.getSystemService()检索此接口的实例。

In your particular case you are interseted in this use case 在您的特定情况下,您对这个用例感到困惑

An input method (IME) implements a particular interaction model allowing the user to generate text. 输入法(IME)实现了特定的交互模型,允许用户生成文本。 The system binds to the current input method that is use, causing it to be created and run, and tells it when to hide and show its UI. 系统绑定到当前使用的输入方法,从而导致其创建和运行,并告诉其何时隐藏和显示其UI。 Only one IME is running at a time. 一次仅运行一个IME。

Also from the description of hideSoftInputFromWindow you can extract 同样从hideSoftInputFromWindow的描述中,您可以提取

public boolean hideSoftInputFromWindow (IBinder windowToken, int flags)

Synonym for hideSoftInputFromWindow(IBinder, int, ResultReceiver) without a result: request to hide the soft input window from the context of the window that is currently accepting input. hideSoftInputFromWindow(IBinder,int,ResultReceiver)的同义词,没有结果:请求从当前正在接受输入的窗口的上下文中隐藏软输入窗口。 Parameters 参量

windowToken IBinder : The token of the window that is making the request, as returned by View.getWindowToken() . windowToken IBinder :发出请求的窗口的令牌,由View.getWindowToken()返回。

flags int : Provides additional operating flags. flags int :提供其他操作标志。 Currently may be 0 or have the HIDE_IMPLICIT_ONLY bit set. 当前可能为0或设置了HIDE_IMPLICIT_ONLY位。

This makes this in your code refer to a View , so that code is part of a class that extends View 这使得this在你的代码是指一个View ,因此代码是一个类的一部分extends View

Here is an example of its usage. 是一个用法示例。

View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM