2013-11-27 121 views
0

嗨,感謝您的幫助。只要按住按鈕,如何執行操作

我需要一個按鈕來執行以下行爲:只要按鈕被保持按下

  • 當按鈕被按下的動作開始(在這種情況下的圖卷軸)

  • 動作繼續(視圖繼續滾動)

  • 當按鈕被釋放時,動作(滾動)停止。

我已經試過了,但不工作:

Button left = (Button) findViewById(R.id.left); 


    left.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      Log.e("","left"); 
      return false; 
     } 
    }); 

回答

3
Button button = (Button) findViewById(R.id.button); 
button.setOnTouchListener(new View.OnTouchListener() { 

@Override public boolean onTouch(View v, MotionEvent event) { 
    switch(event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
//Start your work 
     break; 
    case MotionEvent.ACTION_UP: 
// stop the work 
     break; 
    case MotionEvent.ACTION_CANCEL: 
// stop the work 
     break; 
    } 
    return false; 
} 



}); 
+0

由於這個工程。不過,我也很努力地按下按鈕,任何想法應該做什麼? – Ishaan

5

我認爲你正在尋找的OnTouchListener組合和MotionEvent.ACTION_DOWN

編輯:

switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       // TODO start scroll 
       break; 
      case MotionEvent.ACTION_UP: 
       // TODO stop scroll 
       break; 
      default: 
       break; 
      } 
      return false; 

希望這有助於.. :)

1

默認情況下,你可以使用onlongclick監聽器,但在你的情況下,它不會給影響作爲其最大時間1-2秒,等按鈕應用運動事件並應用邏輯..

1

您可以使用MotionEvent.ACTION_DOWNMotionEvent.ACTION_UP檢測動作上下

@Override 
    public boolean onTouchEvent(MotionEvent event) { 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      //start action 
      break; 

     case MotionEvent.ACTION_UP: 
      //stop action 
      break; 
     } 
     return super.onTouchEvent(event); 
    } 
1

做出新的滾動視圖

ScrollView sv=new ScrollView(this); 

和當你點擊/開始按鈕。然後開始增加int值和滾動的線程,然後開啓關鍵動作。停止線程。

//start thread which incrment value one by one 
sv.scrollBy(x, y); //x the amount of pixels to scroll by horizontally 
      //y the amount of pixels to scroll by vertically 
+0

It Works ......... – Jone

5

使用MotionEvent.ACTION_DOWNMotionEvent.ACTION_UP檢測處分,以及向下

@Override 
    public boolean onTouchEvent(MotionEvent event) { 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      //start action here 
      break; 


     } 
     return super.onTouchEvent(event); 
    }