我在Android Studio项目中有一个按钮,我希望按钮执行三个不同的操作。首先,单击时。其次,当它被压低到最后时,当它被释放时。我尝试使用onTouch侦听器:
button.setOnTouchListener (new View.OnTouchListener){
@Override
public void onTouch (MotionEvent motionEvent)
switch (motionEvent.getAction ()){
case MotionEvent.ACTION_DOWN:
//record video
return true;
case MotionEvent.ACTION_UP:
//stop record
return true;
}
return true;
}
}
button.setOnClickListener(new View.OnClickListener){
@Override
public void onClick (View view){
//capture image
}
}
问题是,当按钮产生执行后挖掘按钮并使我的应用程序崩溃
ontouchlistener应该提供帮助,以下代码应能够:
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED
return true; // if you want to handle the touch event
}
return false;
}
});