我的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
}
}
问题是,当按钮被点击一旦它产生一个execption并导致我的应用程序崩溃
答案 0 :(得分:-1)
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;
}
});