问题
I\'m developing an Android 2.2.2 application for a client and he wants to do the following:
Now I have a button with an onClick event but he doesn\'t like, he wants to dectect when user release the button.
I\'ve found View.OnTouchListener which I think this is what I need to use but, is there any posibility to add this event to xml like I did with onClick?
<ImageButton
android:id=\"@+id/btnSaveNewGate\"
android:layout_width=\"@dimen/btnSaveNewGate_width\"
android:layout_height=\"@dimen/btnSaveNewGate_height\"
android:layout_below=\"@+id/radioGrGateType\"
android:layout_centerHorizontal=\"true\"
android:layout_marginTop=\"@dimen/btnSaveNewGate_marginTop\"
android:background=\"@null\"
android:contentDescription=\"@string/layout_empty\"
android:onClick=\"onSaveNewGateClick\"
android:scaleType=\"fitXY\"
android:src=\"@drawable/save_gate_selector\" />
I have two questions more:
Which is the event associated when user releases his finger?
Is there any guidelines which prohibit using View.OnTouchListener instead of onClick?
回答1:
The event when user releases his finger is MotionEvent.ACTION_UP. I'm not aware if there are any guidelines which prohibit using View.OnTouchListener instead of onClick(), most probably it depends of situation.
Here's a sample code:
imageButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
// Do what you want
return true;
}
return false;
}
});
回答2:
Presumably, if one wants to use an OnTouchListener rather than an OnClickListener, then the extra functionality of the OnTouchListener is needed. This is a supplemental answer to show more detail of how an OnTouchListener can be used.
Define the listener
Put this somewhere in your activity or fragment.
private View.OnTouchListener handleTouch = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("TAG", "touched down");
break;
case MotionEvent.ACTION_MOVE:
Log.i("TAG", "moving: (" + x + ", " + y + ")");
break;
case MotionEvent.ACTION_UP:
Log.i("TAG", "touched up");
break;
}
return true;
}
};
Set the listener
Set the listener in onCreate (for an Activity) or onCreateView (for a Fragment).
myView.setOnTouchListener(handleTouch);
Notes
getXandgetYgive you the coordinates relative to the view (that is, the top left corner of the view). They will be negative when moving above or to the left of your view. UsegetRawXandgetRawYif you want the absolute screen coordinates.- You can use the
xandyvalues to determine things like swipe direction.
回答3:
OnClick is triggered when the user releases the button. But if you still want to use the TouchListener you need to add it in code. It's just:
myView.setOnTouchListener(new View.OnTouchListener()
{
// Implementation;
});
回答4:
for use sample touch listener just you need this code
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, null, 0);
return true;
}
来源:https://stackoverflow.com/questions/11690504/how-to-use-view-ontouchlistener-instead-of-onclick