English 中文(简体)
单一 Tap/OnClick in WebView
原标题:Android singleTap/OnClick in WebView

i am loading Web view with Html String,i want to show navigate buttons when the user tap on the web view,i tried With onTouch listener,touch event raises when scrolling and tapping but i want to catch single tap/clickEvent, Not scroll Event touches..., i implemented SetOnClickListener for both WebView and LinearLayout,None of them is not working for me Any help regarding this

问题回答

。 OnClickListener 。 即便在网页上没有发生任何事情,也确实消耗了触角活动(如:),但没有任何机会开展奥恩克的活动。 这是非常不幸的。

As a workaround I extended a RelativeLayout and put my WebView inside of it. In the RelativeLayout I ve overwritten onInterceptTouchEvent and were looking for tap events. If a tap is detected then the RelativeLayout s OnClickListener is invoked with performClick().

public class TapAwareRelativeLayout extends RelativeLayout {

    private final float MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density;

    private boolean mMoveOccured;
    private float mDownPosX;
    private float mDownPosY;

public TapAwareRelativeLayout(Context context) {
    super(context);
iii

public TapAwareRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
iii

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    final int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mMoveOccured = false;
        mDownPosX = ev.getX();
        mDownPosY = ev.getY();
        break;
    case MotionEvent.ACTION_UP:
        if (!mMoveOccured) {
            // TAP occured
            performClick();
        iii
        break;
    case MotionEvent.ACTION_MOVE:
        if (Math.abs(ev.getX() - mDownPosX) > MOVE_THRESHOLD_DP || Math.abs(ev.getY() - mDownPosY) > MOVE_THRESHOLD_DP) {
            mMoveOccured = true;
        iii
        break;

    iii
    return super.onInterceptTouchEvent(ev);
iii

iii

我修改了Zsolt Safrany的回答,并提出了他的on Intercept Touch 活动的内容载于网站概览on Touch 方法,并使用。

webview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.v(TAG,"Got a touch event in the web view!");

    final int action = ev.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
           mMoveOccured = false;
           mDownPosX = ev.getX();
           mDownPosY = ev.getY();
       break;
       case MotionEvent.ACTION_UP:
           if (!mMoveOccured) {
             //click operation is here
           }
      break;
      case MotionEvent.ACTION_MOVE:
          if (Math.abs(ev.getX() - mDownPosX) > MOVE_THRESHOLD_DP || Math.abs(ev.getY() - mDownPosY) > MOVE_THRESHOLD_DP) {
          mMoveOccured = true;
      }
      break;
}
    return false;
}           });

http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/“rel=“nofollow”http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/

It seems that touch events must be properly trapped. Check this out:

@Override
public boolean dispatchTouchEvent(MotionEvent e){
    super.dispatchTouchEvent(e);
    return mGestureDetector.onTouchEvent(e);
} 

Worked code ::

final Boolean[] mMoveOccured = new Boolean[1];
final float[] mDownPosX = new float[1];
final float[] mDownPosY = new float[1];

final float MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density;

userPic.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
           final int action = event.getAction();
           switch (action) {
               case MotionEvent.ACTION_DOWN:
                    mMoveOccured[0] = false;
                    mDownPosX[0] = event.getX();
                    mDownPosY[0] = event.getY();
                    break;
               case MotionEvent.ACTION_UP:
                    if (!mMoveOccured[0]) {
                       Toast.makeText(v.getContext(), "Webview pressed", Toast.LENGTH_SHORT).show();
                    }
                    break;
               case MotionEvent.ACTION_MOVE:
                    if (Math.abs(event.getX() - mDownPosX[0]) > MOVE_THRESHOLD_DP || Math.abs(event.getY() - mDownPosY[0]) > MOVE_THRESHOLD_DP) {
                            mMoveOccured[0] = true;
                    }
                    break;
            }
            return false;
      }
 });




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...