我希望在webView上获得Onclick事件:
webView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:1)
您的代码块会在Web视图上单击页面上的项目,但您可以使用onTouchListener
mWebView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
//ontouch
break;
}
return false;
}
});
答案 1 :(得分:1)
public class MyActivity extends Activity implements OnTouchListener{
private float mDownPosX;
private float mDownPosY;
private float mUpPosX;
private float mUpPosY;
private WebView webView;
private float MOVE_THRESHOLD_DP;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_layout);
MOVE_THRESHOLD_DP = 20.0F * act.getResources().getDisplayMetrics().density;
webView v = (WebView) findViewById(R.id.webView);
v.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.mDownPosX = event.getX();
this.mDownPosY = event.getY();
break;
case MotionEvent.ACTION_UP:
this.mUpPosX = event.getX();
this.mUpPosY = event.getY();
if ((Math.abs(mUpPosX - this.mDownPosX) < MOVE_THRESHOLD_DP) && (Math.abs(mUpPosY - this.mDownPosY) < MOVE_THRESHOLD_DP)) {
//your click code here
}
break;
}
return false;
}
}
答案 2 :(得分:0)
答案 3 :(得分:0)
WebView具有唯一的onTouchListener,不是onClickListener,但是,您可以在TouchListener的帮助下创建自己的自定义ClickListener,因此,我为webView创建了一个CustomTouchListener,如下所示,
public class WebViewTouchClickListener implements View.OnTouchListener {
private static int MAX_CLICK_DURATION = 200;
private static int MAX_CLICK_DISTANCE = 15;
private long pressStartTime = 0;
private float pressedX = 0;
private float pressedY = 0;
private boolean stayedWithinClickDistance = false;
private Context context;
private TouchClickCallBack touchClickCallBack;
public WebViewTouchClickListener(Context context, TouchClickCallBack callback) {
this.context = context;
this.touchClickCallBack = callback;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
pressStartTime = System.currentTimeMillis();
pressedX = motionEvent.getX();
pressedY = motionEvent.getY();
stayedWithinClickDistance = true;
break;
case MotionEvent.ACTION_MOVE:
if (stayedWithinClickDistance && distance(pressedX, pressedY, motionEvent.getX(), motionEvent.getY()) > MAX_CLICK_DISTANCE) {
stayedWithinClickDistance = false;
}
return true;
case MotionEvent.ACTION_UP:
long pressDuration = System.currentTimeMillis() - pressStartTime;
if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance) {
touchClickCallBack.isClicked();
}
}
return false;
}
private float distance(float x1, float y1, float x2, float y2) {
double dx = x1 - x2;
double dy = y1 - y2;
double distanceInPx = Math.sqrt(dx * dx + dy * dy);
double distanceInDp = distanceInPx / context.getResources().getDisplayMetrics().density;
return (float) distanceInDp;
}
interface TouchClickCallBack {
void isClicked(); //This interface method will give callback in activity or fragment were we want to use
}
}
然后在活动中实施isClicked()方法以打开数据并按如下所示传递值
webView.setOnTouchListener(new WebViewTouchClickListener(context, new WebViewTouchClickListener.TouchClickCallBack {
@Override
void isClicked() {
//TODO: implement the method for doing the stuff based on web view click.
}))