2015-12-31 29 views
2

我在做一個應用程序,其中我的要求是評級欄應始終有觸摸事件給予評級,但它不應該有拖動功能給評級所以有什麼辦法,如何在Android評分欄中禁用拖動功能。如何在Android評分欄中禁用拖動功能

+0

您可以在XML用這個例子 機器人:isIndicator = 「真」 也加入這一行代碼 ratingBar.setFocusable(假); – ManiTeja

回答

3

要禁用拖動功能,請在RatingBar上執行onMotionEvent()偵聽器。然後,獲取ACTION_DOWN上的x座標,並與ACTION_UP的x座標比較。如果有很大差異(拖動發生),則返回true以處理事件;否則返回false。

例子:

RatingBar bar = (RatingBar) findViewById(R.id.rating_bar); 
bar.setOnTouchListener(new View.OnTouchListener() 
{ 
    private float downXValue; 

    @Override 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) 
     { 
      downXValue = event.getX(); 
      return false; 
     } 

     if(event.getAction() == MotionEvent.ACTION_MOVE) 
     { 
      // When true is returned, view will not handle this event. 
      return true; 
     } 

     if(event.getAction() == MotionEvent.ACTION_UP) 
     { 
      float currentX = event.getX(); 
      float difference = 0; 
      // Swipe on left side 
      if(currentX < downXValue) 
       difference = downXValue - currentX; 
      // Swipe on right side 
      else if(currentX > downXValue) 
       difference = currentX - downXValue; 

      if(difference < 10) 
       return false; 

      return true; 
     } 
     return false; 
    } 
}); 
+0

很好的解決方案,謝謝。 –