2012-06-12 73 views
0

如果我點擊'',它將打印「點擊」。但是,一旦我「長時間點擊」,它將只打印「長按」,而不管長度。LongClick狀態卡住

LinearLayout ll = new LinearLayout(this); 

ll.setOnClickListener(new OnClickListener(){ 
    @Override 
    public void onClick(View v) 
     System.out.println("click"); 
    } 
}); 

ll.setOnLongClickListener(new OnLongClickListener(){ 
    @Override 
    public boolean onLongClick(View v) { 
     v.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         System.out.println("long click");     
        case MotionEvent.ACTION_MOVE: 
        case MotionEvent.ACTION_UP: 
       } 
       return false; 
      }      
     });      
     return true; 
    } 
}); 
+0

我對你的代碼有點困惑。最終,試圖做什麼? – Sam

+0

當我長時間點擊LinearLayout時,我想最終捕獲座標。就這樣,在我長時間點擊一次之後,它會捕獲我點擊的座標。 – Roger

回答

0

只是一個TouchListener怎麼樣?它將在ACTION_DOWN上記錄時間,如果時間足夠長,則從MotionEvent中拉出座標。

ll.setOnTouchListener(new View.OnTouchListener() { 
    Calendar down; 
    public boolean onTouch(View v, MotionEvent event) { 
     switch(event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      down = Calendar.getInstance(); 
      break; 
     case MotionEvent.ACTION_UP: 
      // Only continue if longer than 2 seconds 
      if(Calendar.getInstance().getTimeInMillis() - down.getTimeInMillis() > 2000) { 
       // Touch coordinates: event.getX(), event.getY() 
      } 
     } 
     return true; 
    } 
}); 
0

可能改變:

ll.setOnLongClickListener(new OnLongClickListener(){ 
    @Override 
     public boolean onLongClick(View v) { 
     v.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
      System.out.println("long click");     
      case MotionEvent.ACTION_MOVE: 
      case MotionEvent.ACTION_UP: 
      } 
     return false; 
     }      
     });      
    return true; 
    } 
}); 

ll.setOnLongClickListener(new OnLongClickListener(){ 
    @Override 
    public boolean onLongClick(View v) { 
     System.out.println("long click"); 
     v.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
        case MotionEvent.ACTION_MOVE: 
        case MotionEvent.ACTION_UP: 
       } 
       return false; 
      }      
     });      
     return true; 
    } 
}); 

這樣一來就會打印出「長按」時,其獲得了長按,而不是觸摸事件,你「設定」之後