android 使EditText始终不弹出软键盘,并且有光标

今天做一个功能,PDA扫描条码内容显示到文本框,但点击EditText时老是弹出软键盘,挡住了一些信息,体验很不好,网上找了很久终于找到最佳效果,参考https://blog.csdn.net/suowolegeca/article/details/49128939

很多说直接设置et_emulator_qrcord.setInputType(InputType.TYPE_NULL); 确实不弹出软键盘,但是不显示光标,最重要是扫描条码也取不到数据,最终使用以下方法完美解决。

 // 获取文本框
  et_emulator_qrcord = (EditText) findViewById(R.id.et_emulator_qrcord);
  //禁止弹出软键盘,并且有光标
  if (android.os.Build.VERSION.SDK_INT <= 10) {
      et_emulator_qrcord.setInputType(InputType.TYPE_NULL);
   } else {
      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
      try {
      //采用反射的方式,调用系统内部方法
         Class<EditText> cls = EditText.class;
         Method setSoftInputShownOnFocus;
         setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
         setSoftInputShownOnFocus.setAccessible(true);
         setSoftInputShownOnFocus.invoke(et_emulator_qrcord, false);
       } catch (Exception e) {
         e.printStackTrace();
       }
   }

 

posted @ 2020-11-09 17:50  有忆  阅读(492)  评论(0编辑  收藏  举报