问题描述
我有一个带有imageView的活动,当用户触摸它时,我在与上一个相同的位置打开了另一个imageView的另一个活动,问题,我需要知道用户是否仍在触摸imageView&自从上一次活动以来,他再也没有没有离开过他的手指了(所以我可以让他用手指来移动新的imageView)?
我试图听触摸事件,但是仅当用户在渲染视图后开始触摸imageView时才触发触摸事件,因此他必须将手指从屏幕上移开&开始再次触摸imageView,我想在此问题中对其进行修复.
我正在使用AndroidAnnotations,因此侦听器代码如下所示:
@Touch(R.id.myImageView)
public void movingImageViewByUser(View view, MotionEvent event) {
// moving the button with the user finger here
}
您需要将触摸事件状态存储在全局变量中,可以在两个活动之间进行访问.
这样,您将需要一个单独的应用程序上下文类,如下所示:
import android.app.Application;
public class GlobalVars extends Application {
public static Boolean mouseDown = false;
}
可以像这样访问变量:
final GlobalVars globs = (GlobalVars)context.getApplicationContext();
globs.mouseDown = true;
因此请记住,这应该是您的onTouchListener可能的样子:
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;
}
});
希望这会有所帮助
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
}
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
这篇关于Android:如何从上次活动中了解用户是否仍在触摸屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!