我正在开发用户需要长时间按住按钮的应用程序。
我如何检测用户的时刻:完成按下或移动他们的触摸位置?
谢谢
最佳答案
我认为您最好的选择是对该按钮使用 onLongClickListener() 和 onTouchListener() 的组合。您需要在触摸监听器上捕获某些事件,因为它会为每个触摸事件触发。
尝试以下方法:
class Blah extends Activity {
private Button mSpeak;
private boolean isSpeakButtonLongPressed = false;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.blahlayout);
Button mSpeak = (Button)findViewById(R.id.speakbutton);
mSpeak.setOnLongClickListener(speakHoldListener);
mSpeak.setOnTouchListener(speakTouchListener);
}
private View.OnLongClickListener speakHoldListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View pView) {
// Do something when your hold starts here.
isSpeakButtonLongPressed = true;
return true;
}
}
private View.OnTouchListener speakTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View pView, MotionEvent pEvent) {
pView.onTouchEvent(pEvent);
// We're only interested in when the button is released.
if (pEvent.getAction() == MotionEvent.ACTION_UP) {
// We're only interested in anything if our speak button is currently pressed.
if (isSpeakButtonLongPressed) {
// Do something when the button is released.
isSpeakButtonLongPressed = false;
}
}
return false;
}
}
}
关于Android - 检测长按结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6183874/