在 Android 10 上以编程方式关闭键盘无法正常工作。

huangapple go评论102阅读模式
英文:

Closing keyboard programmatically not working on Android 10

问题

我已在我的API 19真实设备和API 23模拟器上进行了测试,这种方法效果很好:

  1. if(activity != null){
  2. View view = activity.getCurrentFocus();
  3. if (view != null){
  4. InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
  5. if(inputManager != null){
  6. inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
  7. }
  8. }
  9. }

然而,在我的Pixel 4a(安卓10)真实设备上无法正常工作。有人知道如何在安卓10上以编程方式关闭键盘吗?

英文:

I've tested it on my API 19 real device and API 23 emulator and this method works fine:

  1. if(activity != null){
  2. View view = activity.getCurrentFocus();
  3. if (view != null){
  4. InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
  5. if(inputManager != null){
  6. inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
  7. }
  8. }
  9. }

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

基于您所处的情况,您可以使用以下方法之一:

  1. boolean hideSuccess = KeyboardUtils.hideKeyboard(activity);
  2. // 或者
  3. boolean hideSuccess = KeyboardUtils.hideKeyboard(fragment);
  4. // 或者
  5. boolean hideSuccess = KeyboardUtils.hideKeyboard(editText); // 推荐使用

KeyboardUtils.java

  1. import android.app.Activity;
  2. import android.view.View;
  3. import android.view.inputmethod.InputMethodManager;
  4. import android.widget.EditText;
  5. import androidx.fragment.app.Fragment;
  6. public class KeyboardUtils {
  7. public static boolean hideKeyboard(Activity activity) {
  8. InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  9. if (imm != null) {
  10. View focus = activity.getCurrentFocus();
  11. if (focus == null) focus = new View(activity);
  12. return imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
  13. } else {
  14. return false;
  15. }
  16. }
  17. public static boolean hideKeyboard(Fragment fragment) {
  18. InputMethodManager imm = (InputMethodManager) fragment.requireContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
  19. if (imm != null) {
  20. View focus = fragment.requireActivity().getCurrentFocus();
  21. if (focus == null) focus = new View(fragment.requireContext());
  22. return imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
  23. } else {
  24. return false;
  25. }
  26. }
  27. public static boolean hideKeyboard(EditText editText) {
  28. InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
  29. if (imm != null) {
  30. return imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
  31. } else {
  32. return false;
  33. }
  34. }
  35. }
英文:

Based on where you are, you can use one of the below methods:

  1. boolean hideSuccess = KeyboardUtils.hideKeyboard(activity);
  2. // or
  3. boolean hideSuccess = KeyboardUtils.hideKeyboard(fragment);
  4. // or
  5. boolean hideSuccess = KeyboardUtils.hideKeyboard(editText); // It's recommended

KeyboardUtils.java

  1. import android.app.Activity;
  2. import android.view.View;
  3. import android.view.inputmethod.InputMethodManager;
  4. import android.widget.EditText;
  5. import androidx.fragment.app.Fragment;
  6. /**
  7. * @author aminography
  8. */
  9. public class KeyboardUtils {
  10. public static boolean hideKeyboard(Activity activity) {
  11. InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  12. if (imm != null) {
  13. View focus = activity.getCurrentFocus();
  14. if (focus == null) focus = new View(activity);
  15. return imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
  16. } else {
  17. return false;
  18. }
  19. }
  20. public static boolean hideKeyboard(Fragment fragment) {
  21. InputMethodManager imm = (InputMethodManager) fragment.requireContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
  22. if (imm != null) {
  23. View focus = fragment.requireActivity().getCurrentFocus();
  24. if (focus == null) focus = new View(fragment.requireContext());
  25. return imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
  26. } else {
  27. return false;
  28. }
  29. }
  30. public static boolean hideKeyboard(EditText editText) {
  31. InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
  32. if (imm != null) {
  33. return imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
  34. } else {
  35. return false;
  36. }
  37. }
  38. }

答案2

得分: 1

尝试以下静态方法:

  1. public static void hideSoftKeyboard(Activity activity) {
  2. InputMethodManager inputMethodManager =
  3. (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  4. View currentFocus = activity.getCurrentFocus();
  5. if (inputMethodManager != null) {
  6. IBinder windowToken = activity.getWindow().getDecorView().getRootView().getWindowToken();
  7. inputMethodManager.hideSoftInputFromWindow(windowToken, 0);
  8. inputMethodManager.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS);
  9. if (currentFocus != null) {
  10. inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
  11. }
  12. }
  13. }
英文:

Try the below static method

  1. public static void hideSoftKeyboard(Activity activity) {
  2. InputMethodManager inputMethodManager =
  3. (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  4. View currentFocus = activity.getCurrentFocus();
  5. if (inputMethodManager != null) {
  6. IBinder windowToken = activity.getWindow().getDecorView().getRootView().getWindowToken();
  7. inputMethodManager.hideSoftInputFromWindow(windowToken, 0);
  8. inputMethodManager.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS);
  9. if (currentFocus != null) {
  10. inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
  11. }
  12. }
  13. }

答案3

得分: 0

在您的清单中添加任务权限。

  1. <uses-permission android:name="android.permission.GET_TASKS"/>
英文:

Add the task permission on your manifest.

  1. &lt;uses-permission android:name=&quot;android.permission.GET_TASKS&quot;/&gt;

huangapple
  • 本文由 发表于 2020年9月19日 16:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63966834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定