我想知道如何使用OnTouchListener。(我在使用Android。)
这是我的代码,当我按下“振动”按钮时,按钮图像不会改变为按下状态:
vibrateButton = (Button) findViewById(R.id.button_vibrate);
vibrationInstance = (Vibrator) getSystemService(this.VIBRATOR_SERVICE);
vibrateButtonPressed = false;
if (!(vibrationInstance.hasVibrator())) {
vibrateButton.setEnabled(false);
}
vibrateButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_UP){
vibrationInstance.cancel();
}
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
vibrationInstance.vibrate(vibrationPattern, 0);
}
return false;
}
});这就是使用OnTouchListener的方式吗?对return false;的需求是什么?谢谢!
发布于 2013-12-14 09:53:58
我想您的应用程序正在崩溃,因为您忘记了将权限添加到清单文件中。确保这是在它中:<uses-permission android:name="android.permission.VIBRATE" />
顺便说一句,如果您想接收所有发生在视图中的触摸事件,您应该在return true中使用OnTouchListener。
https://stackoverflow.com/questions/20581391
复制相似问题