Android 中View僅僅能接收到ACTION_DOWN無法接收ACTION_MOVE和ACTION_UP解決的方法


昨天晚上調試了一晚上,在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" />


在這里記下了。也給別人提供一個方便



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM