Android 事件觸發(fā)機(jī)制的深入學(xué)習(xí)
成都創(chuàng)新互聯(lián)專注于木蘭企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城系統(tǒng)網(wǎng)站開發(fā)。木蘭網(wǎng)站建設(shè)公司,為木蘭等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
最近在研究android的事件觸發(fā)和傳播機(jī)制,說(shuō)來(lái)很慚愧,web下的事件太熟悉不過(guò)了,可在android中卻很郁悶,常用的觸摸事件都糊里糊涂的,在網(wǎng)上看了半天,也整理一份,供大家參考:
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, 按下時(shí)觸發(fā)[" + arg0.getX() + "," + arg0.getY() + "]"); return true; } @Override public void onLongPress(MotionEvent arg0) { Log.e("-------", "onLongPress,長(zhǎng)時(shí)間按住時(shí)觸發(fā)"); } @Override public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { Log.e("-------", "onScroll,滑動(dòng)拖動(dòng)事件[" + 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, 滑動(dòng)甩出事件"); return true; } @Override public void onShowPress(MotionEvent arg0) { Log.e("-------", "onShowPress,down事件發(fā)生,而move或up還沒(méi)發(fā)生前觸發(fā)該事件"); } @Override public boolean onSingleTapUp(MotionEvent arg0) { Log.e("-------", "onSingleTapUp 釋放事件"); return true; } } }
監(jiān)控觸摸事件,主要是實(shí)現(xiàn)OnGestureListener類,并重寫其中的幾個(gè)方法,以監(jiān)聽事件發(fā)生。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!