昨天晚上調試了一晚上,在LinearLayout上接收屏幕動作,但是出現了問題, 下面的代碼是本人調的代碼
</pre><pre name="code" class="java">private int pressedArrow;
public class onSetterTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
float pt = event.getX();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (pt > last_strpos && pt < last_endpos) {
if (pt - last_strpos <= last_endpos - pt) {
cur_strpos = pt;
pressedArrow = R.id.start_pos;
} else {
cur_endpos = pt;
pressedArrow = R.id.end_pos;
}
} else if (pt <= last_strpos) {
cur_strpos = pt;
pressedArrow = R.id.start_pos;
} else if (pt >= last_endpos) {
cur_endpos = pt;
pressedArrow = R.id.end_pos;
}
requestLayout();
Log.d(TAG, "down " + cur_strpos + " " + cur_endpos);
last_endpos = cur_endpos;
last_strpos = cur_strpos;
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "MOVE " + cur_strpos + " " + cur_endpos);
if (pressedArrow == R.id.start_pos) {
if (pt < last_endpos)
cur_strpos = pt;
} else if (pressedArrow == R.id.end_pos) {
if (pt > last_strpos)
cur_endpos = pt;
}
requestLayout();
last_endpos = cur_endpos;
last_strpos = cur_strpos;
break;
case MotionEvent.ACTION_UP:
updateCalibrator((int)cur_strpos);
break;
}
return false;
}
}
這樣一看這個OnTouchListener 的複寫應該沒什麼問題,結尾處return false 代表該事件在此處已經被消費了,可是打開DDMS查看打印日誌,當手指滑動到設置了上面Touch監聽器的ImageView時,總是打印不出來ACTION_MOVE這裏,很明顯是根本沒有進去,後來各種查問題都查不到,因爲ACTION_DOWN是可以進入的,但是ACTION_MOVE和ACTION_UP卻不行,已經不是代碼的問題了。
各種查找才發現,要把設置監聽的這個View的屬性設置成 android:clickable="true" 這樣纔可以響應ACTION_MOVE和ACTION_UP了。
<ImageView
android:id="@+id/track"
android:layout_width="match_parent"
android:layout_height="20dp"
android:clickable="true"
android:layout_gravity="center"
android:background="#00000000"
android:baselineAlignBottom="true"
android:visibility="visible" />
在這裏記下了,也給別人提供一個方便