ScrollView嵌套EditText,这会有什么问题呢,我这里说的是当EditText内输入多行内容,当EditText所在布局不够的时候,必然需要滑动查看输入的所有内容。可是当我们把EditText放在ScrollView里面的时候,会发现EditText的滑动失效了。

原因就是触摸事件被ScrollView给拦截了,解决方法有两种。一种是自定义ScrollView,让ScrollView不去拦截子view的触摸事件,另一种是让EditText在滑动的时候,不让父view去拦截。

接下来通过代码说明这两种方式: 方式一:自定义ScrollView,重新onInterceptTouchEvent方法

public class MyScrollView extends ScrollView { public MyScrollView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MyScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } //重写该方法,让ScrollView不进行事件拦截 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return false; } }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
第二种方式:自定义EditText,重写dispatchTouchEvent方法

public class MyEditText extends EditText { public MyEditText(Context context) { super(context); // TODO Auto-generated constructor stub } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent ev) { //让父类不不拦截自己的触摸事件 getParent().requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); } }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
两种方式,都可以让EditText在滑动的时候,不被父view拦截触摸事件,执行自己的滑动操作。 当然,如果我们在ScrollView里面还有其他的可以滚动的控件,使用第一种方式还可能会带来其他问题。

比如当我们在里面放置一个HorizontalScrollView的时候,在竖直方向滑动该控件的位置,外面的ScrollView并不会起作用,它会让HorizontalScrollView自己去处理触摸事件,所以在这种情况下,推荐使用第二种方式。

public class MyScrollView extends ScrollView {

// 滑动距离及坐标

private float xDistance, yDistance, xLast, yLast;

public MyScrollView(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

switch (ev.getAction()) {

case MotionEvent.ACTION_DOWN:

xDistance = yDistance = 0f;

xLast = ev.getX();

yLast = ev.getY();

break;

case MotionEvent.ACTION_MOVE:

final float curX = ev.getX();

final float curY = ev.getY();

xDistance += Math.abs(curX - xLast);

yDistance += Math.abs(curY - yLast);

xLast = curX;

yLast = curY;

if(xDistance > yDistance){

return false;

}

}

return super.onInterceptTouchEvent(ev);

}

}