Android 事件觸發(fā)機(jī)制的深入學(xué)習(xí)
創(chuàng)新互聯(lián)長(zhǎng)期為近千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為虎丘企業(yè)提供專業(yè)的網(wǎng)站制作、網(wǎng)站設(shè)計(jì),虎丘網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。最近在研究android的事件觸發(fā)和傳播機(jī)制,說來很慚愧,web下的事件太熟悉不過了,可在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還沒發(fā)生前觸發(fā)該事件"); } @Override public boolean onSingleTapUp(MotionEvent arg0) { Log.e("-------", "onSingleTapUp 釋放事件"); return true; } } }