Android beginner: understanding MotionEvent actions

后端 未结 2 461
甜味超标
甜味超标 2020-12-15 23:36

I am having trouble getting my activity to generate a MotionEvent.ACTION_UP. Probably a beginner\'s error.

In LogCat, I\'m only seeing the ACTION_MOVE event (which i

相关标签:
2条回答
  • 2020-12-16 00:25

    First you need to follow the Webview tutorial to let a WebViewClient instead of the default browser do the work. Without that, after you load a web page, you would not get any events at all.

    Assuming you want to handle the events in your acrtivity you can add the method dispatchTouchEvent:

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.v("FROM_DISPATCH_TOUCH_EVENT", "Action = " + ev.getAction());
        return false; 
    }
    

    You will see the other touch events as well. You should handle them in this method.

    An alternative approuch is to register an EventListener. Your class would become:

    public class Brand extends Activity implements OnTouchListener {
    

    in onCreate() do this:

    webview.setOnTouchListener(this);
    

    an add this to the Activity:

    @Override
    public boolean onTouch(View v, MotionEvent event) {
       int action = event.getAction();
        float x = event.getX();
        float y = event.getY();
    
        Log.v("ON_TOUCH", "Action = " + action + " View:" + v.toString());
        Log.v("ON_TOUCH", "X = " + x + "Y = " + y);
    
    return false;
    

    }

    From my logs I then get the following:

    1. dispatchTouchEvent() is called first
    2. Then onTouch() is called
    3. Then the View gets the Event to handle it
    4. Then onTouchEvent() might get events (see below)

    Moreover, onTouch() reports relative to the View, dispatchTouchEvent() reports values relative to the Display.

    Why does onTouchEvent not report these Events? Like many of these methods there are two incarnations of onTouchEvent().

    If you override onTouchEvent() in your Activity it serves as a "catch all" - it only handles the events that have not been consumed by the Views, for example if you tap outside of any View, or the events these Views pass on. If you override onTouchEvent() in your View it it supposed to handle the events in that View. Sometimes it is easy to confuse the two.

    0 讨论(0)
  • 2020-12-16 00:32

    Your problem is that you're doing this from within a WebView. WebViews have built-in touch control to allow the user to scroll the page around, and that must be interfering with you receiving the MotionEvents. It would seem that you will need to use a different View. For example, I just ran your code making the following substitution:

    TextView tv = new TextView(this);
    setContentView(tv);
    

    instead of

    webview = new WebView(this);
    setContentView(webview);
    webview.loadUrl("file:///android_asset/Brand.html");
    

    and everything else worked. Note: I used a TextView as the whole view just for simplicity. This works on ViewGroups like LinearLayouts, RelativeLayouts, custom Views with canvases etc. It just seems that WebViews are special. (I was also only receiving ACTION_MOVE when using the WebView.) So, I hope you can get away with using something other than a WebView!

    0 讨论(0)
提交回复
热议问题