Android: how to know if user is still touching screen from last activity

我只是一个虾纸丫 提交于 2019-12-12 06:20:21

问题


I've an activity with an imageView, when user touches it, I open another activity with another imageView in the same place as the previous one, question is, I need to know if the user is still touching the imageView & never left his finger of the screen since the last activity (so I can let him move the new imageView around with his finger)?

I tried to listen to the touch event, but touch event fires only when user starts touching the imageView after the view is being rendered, so he has to left his finger off the screen & start touching the imageView again, which I want to fix in this question.

I'm using AndroidAnnotations, so the listener code goes like this:

@Touch(R.id.myImageView)
public void movingImageViewByUser(View view, MotionEvent event) {
    // moving the button with the user finger here
}

回答1:


You'll need to store the touch event status in a global variable, that can be accessed between both activities.

With that, you'll need a separate Application-context class, like so:

import android.app.Application;

public class GlobalVars extends Application {
    public static Boolean mouseDown = false;
}

The variable can be accessed like so:

final GlobalVars globs = (GlobalVars)context.getApplicationContext();
globs.mouseDown = true;

So with that in mind, this should be what your onTouchListener might look like:

view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v,MotionEvent event) {
        final GlobalVars globs = (GlobalVars)context.getApplicationContext();
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                globs.mouseDown = true;
                break;
            case MotionEvent.ACTION_UP:
                globs.mouseDown = false;
                break;
        }
        return true;
    }
});

Hope this helps



来源:https://stackoverflow.com/questions/29256697/android-how-to-know-if-user-is-still-touching-screen-from-last-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!