2012-02-12 52 views
2

我使用的是要充当一个指示器的自定义风格的RatingBar,但是我希望用户仍然能够点击的RatingBar带来了一个自定义对话框用的RatingBar那可以改变。的Android的RatingBar的按钮时isIndicator =真

反正有没有做到这一点,而不使用的图像?

举例说明:

RatingBar Highlighed

回答

2

对于那些谁是具有相同问题的道路,我想它了。

ratingBar = (RatingBar) findViewById(R.id.rbMyRating); 
ratingBar.setIsIndicator(true); 
ratingBar.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_UP) { 
      myRatingDialog.show(); 
      v.setPressed(false); 
     } 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      v.setPressed(true); 
     } 
     if (event.getAction() == MotionEvent.ACTION_CANCEL) { 
      v.setPressed(false); 
     } 
     return true; 
    } 
}); 

关键是你仍然可以使用setPressed为true,即使isIndicator为true。这将允许只允许RatingBar强调但不能改变。

您可以管理使用OnTouchListener的setPressed状态。 ACTION_UP和ACTION_DOWN控制窗口小部件上的触摸事件,ACTION_CANCEL处理窗口小部件以外的任何事件。


上述代码的问题是它不支持按键事件。要做到这一点,需要以下额外代码:

Float rating = 4f; 
ratingBar.setFocusable(true); 
ratingBar.setTag(rating); 
ratingBar.setOnKeyListener(new OnKeyListener() { 
    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     if (event.getAction() == KeyEvent.ACTION_DOWN) { 
      if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) { 
       v.setPressed(false); 
       myRatingDialog.show(); 
      } else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { 
       if (v.focusSearch(View.FOCUS_LEFT) != null) v.focusSearch(View.FOCUS_LEFT).requestFocus(); 
      } else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { 
       if (v.focusSearch(View.FOCUS_RIGHT) != null) v.focusSearch(View.FOCUS_RIGHT).requestFocus(); 
      } 
     } 
     return false; 
    } 
}); 
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { 
    @Override 
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 
     ratingBar.setRating((Float) ratingBar.getTag()); 
    } 
}); 

上面的代码同时使用OnKeyListener和setOnRatingBarChangeListener。要启用关键事件,您需要设置setFocusable(true)。您还需要手动连接KEYCODE_ENTER和KEYCODE_DPAD_CENTER的关键事件。

setFocusable = true的问题是您现在可以使用KEYCODE_DPAD_LEFT和KEYCODE_DPAD_RIGHT更改RatingBar评级。要解决此问题,请使用onRatingBarChangeListener重置RatingBar的评分。您可以将评分存储在RatingBar的标签中。请记住,当您通过对话框更改评分时,您需要更改标签值。

由于使用onRatingBarChangeListener你现在将面临一个最后一个问题的结果。既然你是自动更改等级,用户将不能够专注与KEYCODE_DPAD_LEFT和KEYCODE_DPAD_RIGHT其他UI元素。要解决这个问题,只需使用OnKeyListener检测左右DPad动作并使用searchFocus和requestFocus更改焦点。请记住,如果没有找到UI元素,searchFocus将返回null。

希望这有助于别人谁运行到这个问题。 :)

0

你可以在一个相对布局添加您的RatingBar并用一个透明的按钮

<RelativeLayout 
android:layout_width:"match_parent" 
android:layout_height:"wrap_content" > 

    <RatingBar 
     android:layout_width:"wrap_content" 
     android:layout_height:"wrap_content" 
     android:numStars="5" 
     android:isIndicator="true" /> 

    <Button 
     android:layout_width:"match_parent" 
     android:layout_height:"wrap_content" 
     android:background:"@android:color/transparent" /> 

</RelativeLayout> 

覆盖它,那么你的按钮添加监听器。