2016-03-07 26 views
0

我碰到这段代码,但我想知道这段代码做了什么,InputMethodManager究竟是什么以及我的类中应该在哪里输入这段代码?它会进入onCreate()方法还是应该创建一个新的方法?再次,我想了解这种方法是如何工作的。在EditText视图中输入后隐藏键盘

感谢提前:)你的答案我感谢帮助

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

回答

2

javadoc of InputMethod是相当描述一下

中央系统API的整体的输入法框架(IMF )体系结构,仲裁应用程序和当前输入方法之间的交互。您可以使用Context.getSystemService()检索此接口的实例。

你的具体情况你interseted在这种使用情况下

的输入法(IME)实现了特定的交互模型,允许用户生成的文本。系统绑定到当前使用的输入方法,导致它被创建并运行,并告诉它何时隐藏和显示其UI。一次只能运行一个IME。

另外从 hideSoftInputFromWindow描述可以不用结果提取

public boolean hideSoftInputFromWindow (IBinder windowToken, int flags)

同义词hideSoftInputFromWindow(的IBinder,INT,ResultReceiver):请求隐藏从上下文软输入窗口当前正在接受输入的窗口。 参数

windowToken IBinder:正在发出请求的窗口的标记,如通过View.getWindowToken()返回。

flags int:提供附加的操作标志。当前可能为0或者设置了HIDE_IMPLICIT_ONLY位。

这使得this在你的代码指View,使代码是一个类的一部分extends View

Here是其使用的例子。

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

请不要添加纯代码的答案。也可以添加关于答案的简要说明。 –