英文:
Closing keyboard programmatically not working on Android 10
问题
我已在我的API 19真实设备和API 23模拟器上进行了测试,这种方法效果很好:
if(activity != null){
View view = activity.getCurrentFocus();
if (view != null){
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if(inputManager != null){
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
然而,在我的Pixel 4a(安卓10)真实设备上无法正常工作。有人知道如何在安卓10上以编程方式关闭键盘吗?
英文:
I've tested it on my API 19 real device and API 23 emulator and this method works fine:
if(activity != null){
View view = activity.getCurrentFocus();
if (view != null){
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if(inputManager != null){
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
However, it's not working on my Pixel 4a (android 10) real device. Does anyone know how to close the keyboard programmatically on android 10?
答案1
得分: 2
基于您所处的情况,您可以使用以下方法之一:
boolean hideSuccess = KeyboardUtils.hideKeyboard(activity);
// 或者
boolean hideSuccess = KeyboardUtils.hideKeyboard(fragment);
// 或者
boolean hideSuccess = KeyboardUtils.hideKeyboard(editText); // 推荐使用
KeyboardUtils.java
import android.app.Activity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import androidx.fragment.app.Fragment;
public class KeyboardUtils {
public static boolean hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null) {
View focus = activity.getCurrentFocus();
if (focus == null) focus = new View(activity);
return imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
} else {
return false;
}
}
public static boolean hideKeyboard(Fragment fragment) {
InputMethodManager imm = (InputMethodManager) fragment.requireContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null) {
View focus = fragment.requireActivity().getCurrentFocus();
if (focus == null) focus = new View(fragment.requireContext());
return imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
} else {
return false;
}
}
public static boolean hideKeyboard(EditText editText) {
InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null) {
return imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
} else {
return false;
}
}
}
英文:
Based on where you are, you can use one of the below methods:
boolean hideSuccess = KeyboardUtils.hideKeyboard(activity);
// or
boolean hideSuccess = KeyboardUtils.hideKeyboard(fragment);
// or
boolean hideSuccess = KeyboardUtils.hideKeyboard(editText); // It's recommended
KeyboardUtils.java
import android.app.Activity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import androidx.fragment.app.Fragment;
/**
* @author aminography
*/
public class KeyboardUtils {
public static boolean hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null) {
View focus = activity.getCurrentFocus();
if (focus == null) focus = new View(activity);
return imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
} else {
return false;
}
}
public static boolean hideKeyboard(Fragment fragment) {
InputMethodManager imm = (InputMethodManager) fragment.requireContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null) {
View focus = fragment.requireActivity().getCurrentFocus();
if (focus == null) focus = new View(fragment.requireContext());
return imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
} else {
return false;
}
}
public static boolean hideKeyboard(EditText editText) {
InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null) {
return imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
} else {
return false;
}
}
}
答案2
得分: 1
尝试以下静态方法:
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
View currentFocus = activity.getCurrentFocus();
if (inputMethodManager != null) {
IBinder windowToken = activity.getWindow().getDecorView().getRootView().getWindowToken();
inputMethodManager.hideSoftInputFromWindow(windowToken, 0);
inputMethodManager.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS);
if (currentFocus != null) {
inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
}
}
英文:
Try the below static method
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
View currentFocus = activity.getCurrentFocus();
if (inputMethodManager != null) {
IBinder windowToken = activity.getWindow().getDecorView().getRootView().getWindowToken();
inputMethodManager.hideSoftInputFromWindow(windowToken, 0);
inputMethodManager.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS);
if (currentFocus != null) {
inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
}
}
答案3
得分: 0
在您的清单中添加任务权限。
<uses-permission android:name="android.permission.GET_TASKS"/>
英文:
Add the task permission on your manifest.
<uses-permission android:name="android.permission.GET_TASKS"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论