如何按下按钮时关闭键盘?Android关闭键盘
回答
要禁用或解除虚拟键盘?
如果你想完全否定它,你可以使用下面的代码行中的按钮的On Click事件
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
+1。像魅力一样工作。 – 2011-08-02 10:15:24
这两个问题....一个是,myEditText需要是最终的。其次是我必须知道哪个EditText框具有焦点。任何解决方案? – 2012-07-16 04:00:38
对于任何在这里绊倒的人,你可以使用活动的(或者你所在的活动或者片段'getActivity()')'getCurrentFocus()。getWindowToken()'将第一个参数设置为'hideSoftInputFromWindow()'。另外,如果您试图在更改活动时让它消失,请在'onPause()'而不是'onStop()'中执行。 – 2013-03-21 19:17:04
,你还可以在按钮的单击事件使用此代码
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
该方法是唯一对我有用的方法,谢谢! – Emilio 2011-09-20 08:58:18
嗯。这对我来说根本不起作用。 – tomwhipple 2012-01-27 01:45:45
不适用于API 23 – 2016-05-10 11:54:38
的InputMethodManager的第一个解决方案就像我的冠军,getWindow()。setSoftInputMode方法没有在Android 4.0.3宏达惊喜。
@Enghan艾伦,我不需要编辑文本的最后。也许你正在使用一个EditText内部类来声明包含方法?您可以使EditText成为Activity的类变量。或者只是在内部类/方法内声明一个新的EditText并再次使用findViewById()。另外,我没有发现我需要知道表单中哪个EditText有焦点。我可以随便挑一个并使用它。像这样:
欢迎使用Stack Overflow!这真是一个评论,而不是一个答案。有了更多的代表,[你将能够发表评论](http://stackoverflow.com/privileges/comment)。 – Jack 2012-10-31 18:15:32
这是一个正确的答案。谢谢安迪! – 2013-01-30 18:36:35
上述解决方案不适用于所有设备,而且它使用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);
}
}
梦幻般的一点,谢谢 – Fattie 2014-05-27 11:43:14
'isAcceptingText()'使这个答案比其他人更好 – user1506104 2017-04-25 12:19:11
这是我的解决方案
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);
}
}
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();
}
}
这里有一个科特林溶液(混合线程的各种答案)
创建扩展功能(可能在一个普通的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()
- 1. UIKeyboardTypeNumberPad关闭键盘
- 2. MFMessageComposeViewController关闭键盘
- 3. 关闭iphone键盘
- 4. 关闭android默认键盘onClick EditText
- 5. 关闭输入焦点的Android键盘
- 6. 关闭键盘数字键盘
- 7. ResignFirstResponder不关闭键盘(iPhone)
- 8. MonoTouch关闭键盘(各种)
- 9. windows 7 phone - 关闭键盘
- 10. applicationWillResignActive关闭键盘iPhone
- 11. 使用键盘关闭JFrame
- 12. Framework7 - SearchBar关闭键盘
- 13. 关闭IPAD上的键盘
- 14. 如何关闭键盘?
- 15. 关闭textview的键盘
- 16. trigger.io - 关闭软键盘?
- 17. Android:如何强制打开软键盘时强制关闭软键盘?
- 18. Android软键盘:如何在键盘上打开/关闭操作视图
- 19. 当点击数字键盘关闭键盘,并把号码
- 20. 在设置包中关闭键盘/数字键盘iOS
- 21. iPad的'解雇键盘'按钮并没有关闭键盘
- 22. 关闭在桌面上敲击键盘
- 23. 关闭片段中的软键盘
- 24. 倾听完成不关闭软键盘
- 25. 不要关闭show-segue上的键盘
- 26. 安卓键盘向后按关闭
- 27. 关闭键盘导航jQuery的prettyphoto
- 28. 在UIAlertView中关闭UITextField的键盘
- 29. 如何失去而不关闭键盘
- 30. Flex 4.6隐藏/关闭软键盘
做一个EditText上调焦=假做这项工作。你想完全禁用它吗? – st0le 2010-08-24 05:49:37