1.废话不多说,直接上代码,如下
package com.example.mygestrue; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; public class MainActivity extends ActionBarActivity { GestureDetector mDetector; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDetector = new GestureDetector(this,new GestureDetector.OnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent arg0) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent arg0) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { // TODO Auto-generated method stub return false; } @Override public void onLongPress(MotionEvent arg0) { // TODO Auto-generated method stub } @Override public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { // TODO Auto-generated method stub return false; } @Override public boolean onDown(MotionEvent arg0) { // TODO Auto-generated method stub return false; } }); tv = (TextView)findViewById(R.id.tv1); tv.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { Log.e("123", "ontouch"); // TODO Auto-generated method stub return false; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub Log.e("123","ontouchevent"); return super.onTouchEvent(event); }; }
从上面的代码我们可以了解两个知识点
1.onTouchEvent()事件,好像只能在控件中继承的时候写死
2.onTouch()事件,可以在代码中给控件注册监听事件
打印的log如下:
1)点击屏幕tv以外的地方
01-02 03:44:14.665: E/123(2845): ontouchevent 01-02 03:44:14.689: E/123(2845): ontouchevent 01-02 03:44:14.706: E/123(2845): ontouchevent 01-02 03:44:14.723: E/123(2845): ontouchevent 01-02 03:44:14.740: E/123(2845): ontouchevent 01-02 03:44:14.758: E/123(2845): ontouchevent 01-02 03:44:14.774: E/123(2845): ontouchevent 01-02 03:44:14.791: E/123(2845): ontouchevent 01-02 03:44:14.809: E/123(2845): ontouchevent 01-02 03:44:14.826: E/123(2845): ontouchevent 01-02 03:44:14.843: E/123(2845): ontouchevent 01-02 03:44:14.860: E/123(2845): ontouchevent 01-02 03:44:14.877: E/123(2845): ontouchevent
2)点击tv的时候
01-02 03:44:18.290: E/123(2845): ontouch 01-02 03:44:18.291: E/123(2845): ontouchevent 01-02 03:44:18.320: E/123(2845): ontouchevent 01-02 03:44:18.340: E/123(2845): ontouchevent 01-02 03:44:18.354: E/123(2845): ontouchevent
得出的结论是
onToucht()事件总是最早接获得监听,然后才是的监听事件onTouchEvent()
2.如果我们把onTouch() 的返回值设置为 true
01-02 03:53:52.923: E/123(3551): ontouch 01-02 03:53:52.954: E/123(3551): ontouch 01-02 03:53:52.971: E/123(3551): ontouch 01-02 03:53:52.985: E/123(3551): ontouch 01-02 03:53:52.988: E/123(3551): ontouch 01-02 03:53:54.701: E/123(3551): ontouch 01-02 03:53:54.729: E/123(3551): ontouch 01-02 03:53:54.746: E/123(3551): ontouch 01-02 03:53:54.755: E/123(3551): ontouch
这个我们可以得出的结论是
在onTouch->onTouchEvent()过程中,前一个(onTouch())返回true,那么这个响应事件就不会在传递给下一个接收着(onTouchEvent()),返回false,就是继续传递
3.现在我们在继续看一下他们两个跟GestureDetector的关系
我们把onTouchEvent()的返回值更改如下
@Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub Log.e("123","ontouchevent"); // return super.onTouchEvent(event); return mDetector.onTouchEvent(event); };
注意一点,虽然GestureDetector监听事件里没有onTouchEvent(),但是它本身却是有这种方法的
打印的log如下
1)点击屏幕tv 外一点
01-02 04:07:39.414: E/123(3808): ontouchevent 01-02 04:07:39.414: E/123(3808): ondown 01-02 04:07:39.443: E/123(3808): ontouchevent 01-02 04:07:39.447: E/123(3808): ontouchevent 01-02 04:07:39.447: E/123(3808): onsingle 01-02 04:07:49.245: E/123(3808): ontouchevent 01-02 04:07:49.246: E/123(3808): ondown 01-02 04:07:49.273: E/123(3808): ontouchevent 01-02 04:07:49.291: E/123(3808): ontouchevent 01-02 04:07:49.293: E/123(3808): ontouchevent
传递顺序onTouchEvent()->GestureDetector.onDown()->GestureDetector.onsingle()
2)滑动屏幕tv外一点
01-02 04:10:34.008: E/123(3808): ontouchevent 01-02 04:10:34.008: E/123(3808): ondown 01-02 04:10:34.056: E/123(3808): ontouchevent 01-02 04:10:34.071: E/123(3808): ontouchevent 01-02 04:10:34.088: E/123(3808): ontouchevent 01-02 04:10:34.089: E/123(3808): onscroll 01-02 04:10:34.106: E/123(3808): ontouchevent 01-02 04:10:34.106: E/123(3808): onscroll 01-02 04:10:34.124: E/123(3808): ontouchevent 01-02 04:10:34.124: E/123(3808): onscroll 01-02 04:10:34.140: E/123(3808): ontouchevent 01-02 04:10:34.140: E/123(3808): onscroll 01-02 04:10:34.157: E/123(3808): ontouchevent 01-02 04:10:34.157: E/123(3808): onscroll 01-02 04:10:34.174: E/123(3808): ontouchevent 01-02 04:10:34.174: E/123(3808): onscroll 01-02 04:10:34.192: E/123(3808): ontouchevent 01-02 04:10:34.192: E/123(3808): onscroll 01-02 04:10:34.208: E/123(3808): ontouchevent 01-02 04:10:34.209: E/123(3808): onscroll 01-02 04:10:34.226: E/123(3808): ontouchevent 01-02 04:10:34.226: E/123(3808): onscroll 01-02 04:10:34.243: E/123(3808): ontouchevent 01-02 04:10:34.243: E/123(3808): onscroll 01-02 04:10:34.260: E/123(3808): ontouchevent 01-02 04:10:34.260: E/123(3808): onscroll 01-02 04:10:34.277: E/123(3808): ontouchevent 01-02 04:10:34.278: E/123(3808): onscroll 01-02 04:10:34.294: E/123(3808): ontouchevent 01-02 04:10:34.294: E/123(3808): onscroll 01-02 04:10:34.311: E/123(3808): ontouchevent 01-02 04:10:34.311: E/123(3808): onscroll 01-02 04:10:34.328: E/123(3808): ontouchevent 01-02 04:10:34.328: E/123(3808): onscroll 01-02 04:10:34.341: E/123(3808): ontouchevent 01-02 04:10:34.341: E/123(3808): onscroll 01-02 04:10:34.342: E/123(3808): ontouchevent 01-02 04:10:34.342: E/123(3808): onfling
传递顺序onTouchEvent()->GestureDetector.onDown()->GestureDetector.onScroll()->GestureDetector.onfling()
对于点击tv,情况跟上面一样,仅仅是先执行ontouch罢了
01-02 04:17:20.804: E/123(3808): ontouch 01-02 04:17:20.805: E/123(3808): ontouchevent 01-02 04:17:20.806: E/123(3808): ondown 01-02 04:17:20.837: E/123(3808): ontouchevent 01-02 04:17:20.854: E/123(3808): ontouchevent 01-02 04:17:20.872: E/123(3808): ontouchevent 01-02 04:17:20.889: E/123(3808): ontouchevent 01-02 04:17:20.906: E/123(3808): ontouchevent 01-02 04:17:20.923: E/123(3808): ontouchevent 01-02 04:17:20.940: E/123(3808): ontouchevent 01-02 04:17:20.957: E/123(3808): ontouchevent 01-02 04:17:20.975: E/123(3808): ontouchevent 01-02 04:17:20.981: E/123(3808): onshowpress 01-02 04:17:20.991: E/123(3808): ontouchevent 01-02 04:17:21.009: E/123(3808): ontouchevent 01-02 04:17:21.026: E/123(3808): ontouchevent 01-02 04:17:21.043: E/123(3808): ontouchevent 01-02 04:17:21.043: E/123(3808): onscroll 01-02 04:17:21.060: E/123(3808): ontouchevent 01-02 04:17:21.063: E/123(3808): ontouchevent
GestureDetector监听时间方法
ondown:这是当按下屏幕的时候执行
onscroll:当屏幕滑动过程中一直时候执行
onfling:滑动屏幕结束,手离开的时候执行
onsingle:点击结束,手离开的时候执行