如何在Android(可能是上下文)的外部类中调用getCurrentFocus()而不是活动 [英] How to call getCurrentFocus() in external class in Android(Maybe Context) instead of activity
本文介绍了如何在Android(可能是上下文)的外部类中调用getCurrentFocus()而不是活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从活动或片段中调用getCurrentFocus()以使结构看起来很漂亮,但是如何调用该方法呢?有时我使用上下文作为参数来实现类似的功能.
I want to call getCurrentFocus() out of activity or fragment to let the structure looks beautiful.But how can I call the method?Sometimes I use context as a parameter to achieve similar function.
推荐答案
您可以使用活动",创建一个名为 Utils 的类,然后将以下代码放入其中.
You can do this by using Activity, Create a class named Utils and put the following code in it.
public class Utils{
public static void hideKeyboard(@NonNull Activity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
现在,您可以在任何Activity中简单地调用此方法以隐藏键盘
Now you can simply call this method in any Activity to hide keyboard
Utils.hideKeyboard(Activity MainActivity.this);
这篇关于如何在Android(可能是上下文)的外部类中调用getCurrentFocus()而不是活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文