編輯:關於Android編程
一、可以直接覆蓋Activity的onTouch方法
public class MainActivity extends Activity { ... // This example shows an Activity, but you would use the same approach if // you were subclassing a View. @Override public boolean onTouchEvent(MotionEvent event){ int action = MotionEventCompat.getActionMasked(event); switch(action) { case (MotionEvent.ACTION_DOWN) : Log.d(DEBUG_TAG,Action was DOWN); return true; case (MotionEvent.ACTION_MOVE) : Log.d(DEBUG_TAG,Action was MOVE); return true; case (MotionEvent.ACTION_UP) : Log.d(DEBUG_TAG,Action was UP); return true; case (MotionEvent.ACTION_CANCEL) : Log.d(DEBUG_TAG,Action was CANCEL); return true; case (MotionEvent.ACTION_OUTSIDE) : Log.d(DEBUG_TAG,Movement occurred outside bounds + of current screen element); return true; default : return super.onTouchEvent(event); } }
二:也可以給單個的view設置監聽器
View myView = findViewById(R.id.my_view); myView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // ... Respond to touch events return true; } });
三:但是 onTouch方法畢竟只能檢測一些簡單的手勢,像滑動、雙擊、長按等,單用onTouch方法處理就顯得棘手了,谷歌提供了方便的 GestureDetector
類來更方便的處理手勢。
具體的需要實例 GestureDetectorCompat 類, 該類中的構造方法需要實現 監聽器。G
estureDetector.OnGestureListener 去通知用戶,當各種事件(滑動、雙擊、長按等)發生。為了使
GestureDetector
正常監聽事件,同時需要重寫Activity的 nTouch方法,把事件交給Detector。具體見官網demo:
public class MainActivity extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{ private static final String DEBUG_TAG = Gestures; private GestureDetectorCompat mDetector; // Called when the activity is first created. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Instantiate the gesture detector with the // application context and an implementation of // GestureDetector.OnGestureListener mDetector = new GestureDetectorCompat(this,this); // Set the gesture detector as the double tap // listener. mDetector.setOnDoubleTapListener(this); } @Override public boolean onTouchEvent(MotionEvent event){ this.mDetector.onTouchEvent(event); // Be sure to call the superclass implementation return super.onTouchEvent(event); } @Override public boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG,onDown: + event.toString()); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { Log.d(DEBUG_TAG, onFling: + event1.toString()+event2.toString()); return true; } @Override public void onLongPress(MotionEvent event) { Log.d(DEBUG_TAG, onLongPress: + event.toString()); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.d(DEBUG_TAG, onScroll: + e1.toString()+e2.toString()); return true; } @Override public void onShowPress(MotionEvent event) { Log.d(DEBUG_TAG, onShowPress: + event.toString()); } @Override public boolean onSingleTapUp(MotionEvent event) { Log.d(DEBUG_TAG, onSingleTapUp: + event.toString()); return true; } @Override public boolean onDoubleTap(MotionEvent event) { Log.d(DEBUG_TAG, onDoubleTap: + event.toString()); return true; } @Override public boolean onDoubleTapEvent(MotionEvent event) { Log.d(DEBUG_TAG, onDoubleTapEvent: + event.toString()); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent event) { Log.d(DEBUG_TAG, onSingleTapConfirmed: + event.toString()); return true; } }
補充:
onScroll:在屏幕上慢慢的滾動。
onFling:在屏幕上快速的滾動,並快速移開手指。
Whether or not you useGestureDetector.OnGestureListener
, it's best practice to implement anonDown()
method that returnstrue
. This is because all gestures begin with anonDown()
message. If you returnfalse
fromonDown()
, asGestureDetector.SimpleOnGestureListener
does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods ofGestureDetector.OnGestureListener
never get called. This has the potential to cause unexpected problems in your app. The only time you should returnfalse
fromonDown()
is if you truly want to ignore an entire gesture.
另外,官網上說最好復寫onDown方法,並返回true,因為其他方法的消息都是從onDown方法開始的。而且onDown系統默認返回false,其他的方法也不會被調用。
可是我自己試了下,當onDown方法返回false,其他方法是可以被調用的。。這就奇怪了。。。哪位高手親試過這個例子的,回復下。。
??Android開源庫自己一直很喜歡Android開發,就如博客簽名一樣, 我是程序猿,我為自己代言 。在摸索過程中,GitHub上搜集了很多很棒的An
1.首先是注冊頁面的布局:<?xml version=1.0 encoding=utf-8?><LinearLayout xmlns:a
大家可能在做app的時候,或多或少需要使用聯系人,而根據google提供的api,你需要編寫大量的代碼,例如首先需要查詢數據庫,涉及到數據庫表和字段以及對應的SQL語句。
每一個Activity組件都有一個關聯的Window對象,用來描述一個應用程序窗口。每一個應用程序窗口內部又包含有一個View對象,用來描述應用程序窗口的視圖。應用程序窗