深圳全飞鸿

标题: 关于android软键盘的专题研究, softinput [打印本页]

作者: zhgc    时间: 2019-11-4 13:11
标题: 关于android软键盘的专题研究, softinput
Activity下所有EditText不弹出软键盘

  1.     public void disableShowInput(EditText editText) {
  2.         if (android.os.Build.VERSION.SDK_INT <= 10) {
  3.             editText.setInputType(InputType.TYPE_NULL);
  4.         } else {
  5.             Class<EditText> cls = EditText.class;
  6.             Method method;
  7.             try {
  8.                 method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
  9.                 method.setAccessible(true);
  10.                 method.invoke(editText, false);
  11.             } catch (Exception e) {//TODO: handle exception
  12.             }
  13.             try {
  14.                 method = cls.getMethod("setSoftInputShownOnFocus", boolean.class);
  15.                 method.setAccessible(true);
  16.                 method.invoke(editText, false);
  17.             } catch (Exception e) {//TODO: handle exception
  18.             }
  19.         }
  20.     }
复制代码

  1.     @Override
  2.     protected void onResume() {
  3.         super.onResume();
  4.         List<View> list = getAllChildViews(this.getWindow().getDecorView());
  5.         for (int i = 0; i < list.size(); i++) {
  6.             if (list.get(i) instanceof EditText)
  7.                 disableShowInput((EditText) list.get(i));
  8.         }
  9.     }
复制代码

  1.     private List<View> getAllChildViews(View view) {
  2.         List<View> allchildren = new ArrayList<View>();
  3.         if (view instanceof ViewGroup) {
  4.             ViewGroup vp = (ViewGroup) view;
  5.             for (int i = 0; i < vp.getChildCount(); i++) {
  6.                 View viewchild = vp.getChildAt(i);
  7.                 allchildren.add(viewchild);
  8.                 //再次 调用本身(递归)
  9.                 allchildren.addAll(getAllChildViews(viewchild));
  10.             }
  11.         }
  12.         return allchildren;
  13.     }
复制代码






欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) Powered by Discuz! X3.2