2013-03-01 254 views
2

我想關閉我的應用程序時,有三個手指觸摸屏幕(就像mac os)。在我的應用程序中,我使用了碎片和查看尋呼機,但我不明白如何使用event.getAction()和Action Mask。用三根手指觸摸後關閉Android應用程序

我所用的兩個

MotionEvent.ACTION_UP 
MotionEvent.ACTION_POINTER_DOWN: 

但他們沒有工作。

當使用不同的ACTION_MASKED應用程序被多次調用時。

int maxPointercount=0; 
int previousPointercount=0; 

public boolean onTouch(View v, MotionEvent event) { 
    int currentpointerCount = event.getPointerCount(); 
    System.out.println("My pointer....." + currentpointerCount); 

    int action = event.getAction() & MotionEvent.ACTION_MASK; 
    System.out.println("pre......."+previousPointercount); 
    System.out.println("max......."+maxPointercount); 

    if(maxPointercount < previousPointercount){ 
     maxPointercount = currentpointerCount; 
    } 

    previousPointercount = currentpointerCount; 

    if(action==MotionEvent.ACTION_) { 
     if(maxPointercount>=3){ 
     maxPointercount = 0; 
     Toast.makeText(MyclassActivity.this,"FingerToched!!"Toast.LENGTH_SHORT).show(); 
     Intent intent = newIntent(MyclassActivity.this,DashboardActivity.class); 
     startActivity(intent); 
     finish(); 
     //your code that will run 1 time 
     } 
     maxPointercount = 0; 
     previousPointercount = 0;  
    } 
    return super.onTouchEvent(event); 
} 
+0

我會質疑爲什麼。如果你想要OSX,那麼使用Mac。我會因爲關閉的應用程序而感到厭煩(我假設你正在做一些像system.exit這樣可怕的事情),只是因爲它檢測到3個手指。 – Simon 2013-03-01 07:52:10

+0

我不明白你想說什麼..BR請用適當的語言解釋.. – Mahi 2013-03-01 07:55:37

+0

@Lars Jensen ..感謝兄弟 – Mahi 2013-03-01 08:02:41

回答

4

這應該有效。

@Override 
    public boolean dispatchTouchEvent(MotionEvent event) { 
     int currentpointerCount = event.getPointerCount(); 
     Log.d("hi", "My pointer....." + currentpointerCount); 

     int action = event.getAction() & MotionEvent.ACTION_MASK; 
     System.out.println("pre......."+previousPointercount); 
     System.out.println("max......."+maxPointercount); 

     if(maxPointercount < previousPointercount){ 
      maxPointercount = currentpointerCount; 
     } 

     previousPointercount = currentpointerCount; 

     if(action==MotionEvent.ACTION_UP) {    
      if(maxPointercount==3){   
      finish(); 
      //your code that will run 1 time 
      } 
      maxPointercount = 0; 
      previousPointercount = 0;  
     } 
     return true; 
     /** This worked for OP's specific case 
     return super.dispatchTouchEvent(event); 
     **/ 
    } 
+0

它正在工作,但是當我使用這段代碼時,我的內心活動已停止。就像查看傳呼機在我的儀表板後使用,但它不能工作,當我應用此代碼...內部佈局不起作用 – Mahi 2013-03-01 09:29:59

+0

您的「內部活動」是什麼意思?使用getActivity()。finish();如果你正在處理碎片。 – 2013-03-01 09:36:17

+0

嗨,夥計們,我在Activity上使用了ondispatched事件。該活動包含視圖尋呼機和tabpageindicator。所以我已經把用完(),但是當我執行ondispatchevent我的活動。該傳呼機和標籤頁指示符剛剛停止工作,包括在當前活動中。 – Mahi 2013-03-01 09:41:29