当我们滑动的时候,就把键盘收起,这样体验感就多好!
“`
要自定义一下ScrollView,监听他的滑动变化,如果用Android 原生的,滑动时候收起键盘不起作用,至于为啥,以后详谈。
public class CustomScrollView extends ScrollView {
HomeScrollView homeScrollView;
public void setHomeScrollView(HomeScrollView homeScrollView) {
this.homeScrollView = homeScrollView;
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context) {
super(context);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (homeScrollView != null) {
homeScrollView.updateScrollUI(t);
}
}
}
- 监听事件
1
1
custtomscrollview.setOnTouchListener(new TouchListenerImpl());
1
1
private class TouchListenerImpl implements View.OnTouchListener {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
//隐藏键盘,,要判断一下是否存在键盘,如果键盘不存在,滑动的是候会报空指针
View view = getWindow().peekDecorView();
if (view != null) {
InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
break;
default:
break;
}
return false;
}
} ;
“`
这样滑动的时候就可以收起键盘了,,哈哈哈哈哈哈,,,,