我正在编写一个需要响应触摸事件的Android应用程序。我希望我的应用程序将我的列表项目的颜色更改为自定义颜色。我写了下面的代码,但只有MotionEvent.ACTION_DOWN
部分正在工作。 LogCat显示ACTION_CANCEL
和ACTION_UP
根本不被调用。你能帮我理解为什么我的代码不工作。不显示MotionEvent.ACTION_UP或MotionEvent.ACTION_CANCEL
这是我的代码...
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
view.setBackgroundColor(Color.rgb(1, 1, 1));
Log.d("onTouch", "MotionEvent.ACTION_UP");
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
view.setBackgroundColor(Color.rgb(23, 128, 0));
Log.d("onTouch", "MotionEvent.ACTION_DOWN");
}
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
view.setBackgroundColor(Color.rgb(1, 1, 1));
Log.d("onTouch", "MotionEvent.ACTION_CANCEL");
}
return false;
}
});
你能不能把日志作为方法的第一行(第一个'if'语句之前)。该方法是否在您期望的所有时间都被触发?此外,如果你正在移动,可能事件得到*批处理*,如http://developer.android.com/reference/android/view/MotionEvent.html – wattostudios 2012-04-17 13:30:03