Android--›EditText不显示系统键盘,可用来显示自定义的键盘

版权声明:欢迎转载,转载请注明出处-->http://blog.csdn.net/angcyo https://blog.csdn.net/angcyo/article/details/83008366

系统键盘 包含普通键盘和现在很多ROM定制的密码安全键盘

调用已下方法即可解决:
https://developer.android.google.cn/reference/android/widget/TextView#setShowSoftInputOnFocus(boolean)

在这里插入图片描述

但是,此方法是API 21Android 5.0加入的, 所以为了兼容低版本, 建议使用已下方法:

 public static final boolean notShowSoftInput(EditText editText) {
      boolean flag = false;

      InputMethodManager imm = (InputMethodManager) editText.getContext()
              .getSystemService(Context.INPUT_METHOD_SERVICE);
      boolean isOpen = imm.isActive();// isOpen若返回true,则表示输入法打开
      if (isOpen) {
          if (imm.hideSoftInputFromWindow(editText.getWindowToken(), 0))
              flag = true;
      }

//		act.getWindow().setSoftInputMode(
//				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
      int currentVersion = android.os.Build.VERSION.SDK_INT;
      String methodName = null;
      if (currentVersion >= 16) {
          // 4.2
          methodName = "setShowSoftInputOnFocus";
      } else if (currentVersion >= 14) {
          // 4.0
          methodName = "setSoftInputShownOnFocus";
      }

      if (methodName == null) {
          editText.setInputType(InputType.TYPE_NULL);
      } else {
          Class<EditText> cls = EditText.class;
          Method setShowSoftInputOnFocus;
          try {
              setShowSoftInputOnFocus = cls.getMethod(methodName,
                      boolean.class);
              setShowSoftInputOnFocus.setAccessible(true);
              setShowSoftInputOnFocus.invoke(editText, false);
          } catch (NoSuchMethodException e) {
              editText.setInputType(InputType.TYPE_NULL);
              e.printStackTrace();
          } catch (IllegalAccessException e) {
              e.printStackTrace();
          } catch (IllegalArgumentException e) {
              e.printStackTrace();
          } catch (InvocationTargetException e) {
              e.printStackTrace();
          }
      }
      return flag;
  }

4.2低版本中有一个setSoftInputShownOnFocus方法, 但是被声明成hide了, 所以通过反射调用.
再低一点的版本,直接通过setInputType的方式兼容.

以上方法调用后, EditText获取到焦点时,就不会弹出系统的键盘了.
然后弹出自定义的键盘,就可以完美解决冲突了.

推荐工程


也许你还想学习更多, 来我的群吧, 我写代码的能力, 远大于写文章的能力:

联系作者

点此快速加群

请使用QQ扫码加群, 小伙伴们都在等着你哦!

关注我的公众号, 每天都能一起玩耍哦!

扫描二维码关注公众号,回复: 5629355 查看本文章

猜你喜欢

转载自blog.csdn.net/angcyo/article/details/83008366