问题
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