In the android application I made a suspension frame layout interface on a button, how do I distinguish a setOnClickListener I still setOnTouchListener event of this button, setOnTouchListener button to make this move, setOnClickListener button click event is triggered, but often only trigger a thing, how can a good solution to this problem, consult experts, paste a few lines of code?<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
setOnTouchListener is monitored from the point of the mouse (possibly dragging the mouse) to release the mouse (the mouse can be replaced with your fingers) the whole process, he's callback function is onTouchEvent (MotionEvent event), and then determine event.getAction () is MotionEvent. ACTION_UP or ACTION_DOWN or ACTION_MOVE respectively, for different behaviors.
setOnClickListener monitoring time to monitor the behavior only occurs when the finger ACTION_DOWN
Reply:
Positive solution, LZ can see for yourself there is no logical sorting correctly
Reply:
Can not setOnClickListener this listener, the listener's content on the ACTION_DOWN to handle onTouchEvent
Reply:
Because I only have to deal with a button click event, but also trigger time onTouchEvent event does not trigger the button onclick event, I really do not know how good deal, look online to determine some of the code is to add a flag to ask that if handled
Reply:
I see the line!
Reply:
Plus semaphore method is to add variables to control some of the code when to go, when to come to the stage.
A typical if / switch statement
Reply:
specifically how to do blanket, you can paste a few lines of code
Reply:
I have a button layout, I want to drag the whole button to change its position, you can click on it to make it triggers the click event, the problem is that the button is clicked it can not distinguish between Or drag it to change how to solve, can be affixed to a few lines of code?
Reply:
You need to understand. Signal is less than the amount of, or less than onclick, with a long record ACTION_MOVE the variable x, y. To determine whether to move the line.
case MotionEvent.ACTION_MOVE:
x = (int) event.getX ();
y = (int) event.getY ();
break;
case MotionEvent.ACTION_UP:
if (x. .. && y ..) {} else {removable handle clicks processing}
PS: x, y values of specific judgments vary due to your layout.
Because sometimes when a finger is pressed move may be small, so you can determine when when X, Y deemed sufficient under normal press small.
Reply:
what is not done to determine
if (event.getX ()-mTouchStartX> 5 | | event.getY ()-mTouchStartY> 5)
{
updateViewPosition ();
mTouchStartX = mTouchStartY = 0;
}
else
{
Utils.startActivity (AlbumActivityXnew.this, Album_photograph.class);
}
break;
Reply:
Are you sure setOnClickListener only monitor ACTION_DOWN behavior? As you say, as long as there Touch behavior, it can trigger setOnclickListener? You can go try, setOnClickListener in the end is not ACTION_DOWNH and ACTION_UP together triggered simply write code into ACTION_DOWN Lane does not realize setOnClickListener effect.
Reply:
mTouchStartX This is when acquired? Do not do subtraction, value ACTION_MOVE event returns can be directly used.
This you can on your device debugging, the effect can be seen.
Reply:
A floor that's right
To the point of it,
Reply:
Will the judge is not doing so
if (event.getX ()-mTouchStartX> 5 | | event.getY ()-mTouchStartY> 5)
{
updateViewPosition ();
mTouchStartX = mTouchStartY = 0;
}
else
{
Utils.startActivity (AlbumActivityXnew.this, Album_photograph.class);
}
break;
Basically it can be understood
In action_move onTouch event, the event moves to determine whether the position exceeds a set value, and sets the flag to distinguish click event
Reply:
My touch event is written not sure, but still no effect, or no distinction between good touch events and click events, this how to change it correctly, ask for advice
Reply:
public boolean onTouch (View v, MotionEvent event) {
/ / TODO Auto-generated method stub
/ / Get coordinates relative to the screen, that is to the upper left corner of the screen as the origin
x = event.getRawX ();
y = event.getRawY () -25; / / 25 is highly
system status barLog.i ("currP", "currX" + x + "==== currY" + y);
switch (event.getAction ()) {
case MotionEvent.ACTION_DOWN:
/ / Get the coordinates relative View, namely the upper left corner as the origin
this ViewmTouchStartX = event.getX ();
mTouchStartY = event.getY ();
Log.i ("startP", "startX" + mTouchStartX + "==== startY" + mTouchStartY);
/ / IsPressed = false;
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition ();
/ / IsPressed = true;
break;
case MotionEvent.ACTION_UP:
/ / Event.getXPrecision ()
if (event.getX ()-mTouchStartX> 5 | | event.getY ()-mTouchStartY> 5)
{
updateViewPosition ();
mTouchStartX = mTouchStartY = 0;
}
else
{
Utils.startActivity (AlbumActivityXnew.this, Album_photograph.class);
}
break;
default:
break;
}
return true;
}
});
Reply:
Ask ah
Reply:
public boolean onTouch (View v, MotionEvent event) {
switch (event.getAction ()) {
case MotionEvent.ACTION_DOWN:
Log.i ("startP", "startX" + mTouchStartX + "==== startY" + mTouchStartY);
/ / IsPressed = false;
break;
case MotionEvent.ACTION_MOVE:
/ / Get the coordinates relative View, namely the upper left corner as the origin
this ViewmTouchStartX = event.getX ();
mTouchStartY = event.getY ();
/ / IsPressed = true;
break;
case MotionEvent.ACTION_UP:
/ / Event.getXPrecision ()
if (mTouchStartX> 5 | | mTouchStartY> 5)
{
updateViewPosition ();
mTouchStartX = mTouchStartY = 0;
}
else
{
Utils.startActivity (AlbumActivityXnew.this, Album_photograph.class);
}
break;
default:
break;
}
return true;
}
});
Try can meet your requirements.
Recommendation: Under a clear call to the timing of various events.
Reply:
ACTION_DOWN ACTION_UP time
constitute a clickWhen onTouch event returns ture will not pass onClick event to event (surface current event has been processed, without passing down)
Just good control onTouch event returns true or false when you will be able to solve the problem
Reply:
MARK about the future use of the ~
Reply:
I tested,
This is because the return value of the callback function caused by the down Touch if it returns false when the event action, then this component will not eat this down event, and follow-up action on the touch is not interested.
This means that if there is a return in onTouch true, then of course it can no longer be onClick callback.
As long as your onTouch function returns false, then the back of the onClick () can enter into force.
Reply:
20th Floor, positive solution, I have to solve the problem of these two events, thank you.
Reply:
You are how to solve ah. I have encountered this problem
Reply:
Solve paste the code, let us also refer
No comments:
Post a Comment