您好,登錄后才能下訂單哦!
Android 事件觸發機制的深入學習
最近在研究android的事件觸發和傳播機制,說來很慚愧,web下的事件太熟悉不過了,可在android中卻很郁悶,常用的觸摸事件都糊里糊涂的,在網上看了半天,也整理一份,供大家參考:
public class CustomDefaultActivity implements OnTouchListener { private GestureDetector gd1; private TableLayout tableLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_default); tableLayout = (TableLayout) findViewById(R.id.tableLayout); gd1 = new GestureDetector(CustomDefaultActivity.this, new MyGesture()); tableLayout.setOnTouchListener(this); tableLayout.setLongClickable(true); } @Override public boolean dispatchTouchEvent(MotionEvent event) { Log.e("-----------------------------------------------------------------------", "dispatchTouchEvent"); if (gd1.onTouchEvent(event)) { event.setAction(MotionEvent.ACTION_CANCEL); } return super.dispatchTouchEvent(event); } @Override public boolean onTouch(View arg0, MotionEvent arg1) { Log.e("-------", "onTouch"); return gd1.onTouchEvent(arg1); } public class MyGesture implements OnGestureListener { @Override public boolean onDown(MotionEvent arg0) { Log.e("-------", "onDown, 按下時觸發[" + arg0.getX() + "," + arg0.getY() + "]"); return true; } @Override public void onLongPress(MotionEvent arg0) { Log.e("-------", "onLongPress,長時間按住時觸發"); } @Override public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { Log.e("-------", "onScroll,滑動拖動事件[" + arg0.getX() + "," + arg0.getY() + "]=[" + arg1.getX() + "," + arg1.getY() + "]"); return true; } @Override public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { Log.e("-------", "onFling, 滑動甩出事件"); return true; } @Override public void onShowPress(MotionEvent arg0) { Log.e("-------", "onShowPress,down事件發生,而move或up還沒發生前觸發該事件"); } @Override public boolean onSingleTapUp(MotionEvent arg0) { Log.e("-------", "onSingleTapUp 釋放事件"); return true; } } }
監控觸摸事件,主要是實現OnGestureListener類,并重寫其中的幾個方法,以監聽事件發生。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。