setOnClickListener not response on Android WebView
I have Android LisView that was contain TextView to display the data in the list,
I add to change it to Webview, after doing that everything look good except the setOnClickListener that not responding anymore..
I have read about the Webview and found that setOnClickListener is not supported, instead setOnTouchListener is supported is there
a way to use the same functionality as setOnClickListener in Android WebView ?
Like this:
myWebView.setOnClickListener(new OnClickListener(){
@开发者_C百科Override
public void onClick(View v) {
//do it ..
}
});
Thanks (:
I ended up with this solution:
public class ClickableWebView extends WebView {
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;
public ClickableWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ClickableWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ClickableWebView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
startClickTime = Calendar.getInstance().getTimeInMillis();
break;
}
case MotionEvent.ACTION_UP: {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if(clickDuration < MAX_CLICK_DURATION) {
super.performClick();
}
}
}
return true;
}
}
Remarks:
- suppresses all click-events to anything inside the
WebView
(e.g.: hyperlinks) - simply add
OnClickListener
by adding anonClick
in xml or in Java - does not interupt scrolling-gestures
Thanks to Stimsoni Answer to How to distinguish between move and click in onTouchEvent()?
Why not use onTouch listener as you stated?
myWebView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
精彩评论