2010-08-24 114 views

回答

283

要禁用或解除虚拟键盘?

如果你想完全否定它,你可以使用下面的代码行中的按钮的On Click事件

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 
+1

+1。像魅力一样工作。 – 2011-08-02 10:15:24

+1

这两个问题....一个是,myEditText需要是最终的。其次是我必须知道哪个EditText框具有焦点。任何解决方案? – 2012-07-16 04:00:38

+66

对于任何在这里绊倒的人,你可以使用活动的(或者你所在的活动或者片段'getActivity()')'getCurrentFocus()。getWindowToken()'将第一个参数设置为'hideSoftInputFromWindow()'。另外,如果您试图在更改活动时让它消失,请在'onPause()'而不是'onStop()'中执行。 – 2013-03-21 19:17:04

13

,你还可以在按钮的单击事件使用此代码

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 
+0

该方法是唯一对我有用的方法,谢谢! – Emilio 2011-09-20 08:58:18

+11

嗯。这对我来说根本不起作用。 – tomwhipple 2012-01-27 01:45:45

+0

不适用于API 23 – 2016-05-10 11:54:38

5

的InputMethodManager的第一个解决方案就像我的冠军,getWindow()。setSoftInputMode方法没有在Android 4.0.3宏达惊喜。

@Enghan艾伦,我不需要编辑文本的最后。也许你正在使用一个EditText内部类来声明包含方法?您可以使EditText成为Activity的类变量。或者只是在内部类/方法内声明一个新的EditText并再次使用findViewById()。另外,我没有发现我需要知道表单中哪个EditText有焦点。我可以随便挑一个并使用它。像这样:

+3

欢迎使用Stack Overflow!这真是一个评论,而不是一个答案。有了更多的代表,[你将能够发表评论](http://stackoverflow.com/privileges/comment)。 – Jack 2012-10-31 18:15:32

+2

这是一个正确的答案。谢谢安迪! – 2013-01-30 18:36:35

50

上述解决方案不适用于所有设备,而且它使用EditText作为参数。这是我的解决方案,只需调用这个简单的方法:

private void hideSoftKeyBoard() { 
    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 

    if(imm.isAcceptingText()) { // verify if the soft keyboard is open      
     imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
    } 
} 
+1

梦幻般的一点,谢谢 – Fattie 2014-05-27 11:43:14

+2

'isAcceptingText()'使这个答案比其他人更好 – user1506104 2017-04-25 12:19:11

22

这是我的解决方案

public static void hideKeyboard(Activity activity) { 
    View v = activity.getWindow().getCurrentFocus(); 
    if (v != null) { 
     InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
    } 
} 
4
public static void hideSoftInput(Activity activity) { 
    try { 
     if (activity == null || activity.isFinishing()) return; 
     Window window = activity.getWindow(); 
     if (window == null) return; 
     View view = window.getCurrentFocus(); 
     //give decorView a chance 
     if (view == null) view = window.getDecorView(); 
     if (view == null) return; 

     InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
     if (imm == null || !imm.isActive()) return; 
     imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    } catch (Throwable e) { 
     e.printStackTrace(); 
    } 
} 
3

这里有一个科特林溶液(混合线程的各种答案)

创建扩展功能(可能在一个普通的ViewHelpers类中)

fun Activity.dismissKeyboard() { 
    val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager 
    if(inputMethodManager.isAcceptingText) 
     inputMethodManager.hideSoftInputFromWindow(this.currentFocus.windowToken, /*flags:*/ 0) 
} 

然后,只需消耗使用:

// from activity 
this.dismissKeyboard() 

// from fragment 
activity.dismissKeyboard()