参考

1、Android打开关闭软键盘
2、android 滑动或者点击其他地方隐藏键盘的方法
3、android中ScrollView的setOnClickListener无效

使用

1、SomeActivity extends BaseActivity
//父控件resid,如果有scrollview是子控件但是match_parent的话用scrollview的residhideParentSoftKeyborad(R.id.sv_batch_review);
2、BaseActivity
/**     * 点击空白处或滑动时候隐藏软键盘     * parent里面下如果有scrollview占据整个画面的话,必须resLayout=scrollview_resid,不然监听不到     * */    protected void hideParentSoftKeyborad(int resLayout){        //以前的:点空白处隐藏软键盘//        findViewById(resLayout).setOnClickListener(new View.OnClickListener() {//            @Override//            public void onClick(View v) {//                LogUtil.d("===hideParentSoftKeyborad = onClick");//                AppUtil.hideSoftKeyboard(instance);//            }//        });        findViewById(resLayout).setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                switch (event.getAction()) {                    case MotionEvent.ACTION_DOWN:                        AppUtil.hideSoftKeyboard(instance);                        break;                    case MotionEvent.ACTION_MOVE:                        AppUtil.hideSoftKeyboard(instance);                        break;                }                return false;            }        });    }
3、AppUtil
/**     * 关闭该控件的软键盘     * */    public static void closeKeyBoard(Context ctx,EditText editText) {        try {            InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);        }        catch (Exception e) {            e.printStackTrace();        }    }    /**     * 隐藏软键盘     * */    public static void hideSoftKeyboard(Activity act){        InputMethodManager manager = (InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE);        View focus = act.getCurrentFocus();        manager.hideSoftInputFromWindow(                focus == null ? null : focus.getWindowToken(),                InputMethodManager.HIDE_NOT_ALWAYS);    }    public static void hideSoftKeyboard(IBinder token, Context context){        InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);        inputMethodManager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);    }
4、ScrollView的坑!!!

1、一般来说直接传parent_resid就可以了。但是!!!
2、scrollview不能onclick监听,但是要监听他的点击和滑动就得用ontouch监听
3、如果parent下有个差不多占据整个屏幕的scrollview请传scrollview_resid比较好,因为parent_resid被遮挡了,监听不到。。。

更多相关文章

  1. 完美解决隐藏Listview和RecyclerView去掉滚动条和滑动到边界阴影
  2. android中listview控件覆盖了其它控件使下面的其它控件不显示
  3. android:windowSoftInputMode属性使用
  4. android:maxLines和android:ellipsize同时使用导致显示异常
  5. Android(安卓)ScrollView 实现小说阅读界面自动滚屏
  6. 在AndroidManifest.xml文件中的android:windowSoftInputMode属性
  7. android:windowSoftInputMode属性的使用
  8. Android(安卓)监听home键(android:launchMode="singleTask" 与 on
  9. inputType属性

随机推荐

  1. Linux I2C内核架构分析,基于三星I2C控制
  2. Linux命令cURL如何访问FTP服务器
  3. 一键获取linux内存、cpu、磁盘IO等信息脚
  4. Ubuntu Linux 安装 .7z 解压和压缩文件
  5. 内核窥探|在kernel中的链表,其他的链表真
  6. Linux wget命令详解
  7. Linux内核 自旋锁spin lock,教你如何用自
  8. linux sed 命令
  9. jQuery:常用 jQuery方法,$()的四种类型参
  10. 虚拟文件系统