我们在做Android开发中,有时候需要用到WebView控件,当你把WebView添加到布局中,你希望在点击整个布局的时候,WebView不接收点击事件,应该向下传递。 举个例子:在ListView中自定义的每一个Item中包含了WebView,你希望点击每一个行能被选中,可是有WebVi
我们在做Android开发中,有时候需要用到WebView控件,当你把WebView添加到布局中,你希望在点击整个布局的时候,WebView不接收点击事件,应该向下传递。
举个例子:在ListView中自定义的每一个Item中包含了WebView,你希望点击每一个行能被选中,可是有WebView的存在,点击事件被拦截了,这个时候该怎么办呢?
解决方法如下:
继承WebView自定义一个MyWebView,然后重载它的onTouchEvent方法,就可以解决了。
代码如下:
public class MyWebView extends WebView{
public MyWebView(Context context, AttributeSet attrs, int defStyle,
boolean privateBrowsing) {
super(context, attrs, defStyle, privateBrowsing);
// TODO Auto-generated constructor stub
}
public MyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyWebView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
Log.d("touchevent", "touchevent"+super.onTouchEvent(ev));
// return super.onTouchEvent(ev);
return false;
}
}