RatingBar不工作

时间:2014-07-05 09:53:09

标签: android ratingbar

我在我的应用程序中有一个简单的评级栏,但是当我触摸星星评价什么都不做时应该很容易但是它不起作用......

我已经尝试了几个教程,但也没有工作。

这是我的代码:

布局代码:

<RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativeLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" 
        android:isIndicator="false"
        android:stepSize="1.0" 
        android:rating="2.0"
        android:numStars="5" 
        android:paddingBottom="5dp" />

的活动:

public final class ViewActivity extends Activity implements OnRatingBarChangeListener {

RatingBar ratingbar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
    ratingbar = (RatingBar) findViewById(R.id.ratingBar1);
    ratingbar.setOnRatingBarChangeListener(this);
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
        boolean fromUser) {
    Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();

}
}

我也尝试过:

的活动:

public final class ViewActivity extends Activity implements OnRatingBarChangeListener {

RatingBar ratingbar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
    ratingbar = (RatingBar) findViewById(R.id.ratingBar1);
OnRatingBarChangeListener rating = new OnRatingBarChangeListener() {    

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {
            Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();
        }
    };
    final RatingBar sBar = (RatingBar) findViewById(R.id.ratingBar1);
    sBar.setOnRatingBarChangeListener(rating);
}

另外:

public final class ViewActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
RatingBar rating = (RatingBar)findViewById(R.id.ratingBar1);// create RatingBar object
    rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {
            Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();
        }
    });

我使用了调试器并在Toast上设置了一个断点,当我触摸星星时没有进入该方法并且也没有显示Toast。

谢谢大家。

2 个答案:

答案 0 :(得分:1)

试试这个:

<强> main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#C2C2A3">

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_marginTop="50dp"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:text="OK" />

<强> MainActivity.java

 public class MainActivity extends Activity
 {
    RatingBar ratingBar;
    Button btn;
 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   ratingBar=(RatingBar)findViewById(R.id.ratingBar1);
   btn=(Button)findViewById(R.id.button1);

   // Set ChangeListener to Rating Bar
    ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating,
         boolean fromUser) {

         Toast.makeText(getApplicationContext(),"Your Selected Ratings  : " + String.valueOf(rating),Toast.LENGTH_LONG).show();

        }
       });

    btn.setOnClickListener(new View.OnClickListener() {


 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  float rating=ratingBar.getRating(); 
  Toast.makeText(getApplicationContext(),"Your Selected Ratings  : " + String.valueOf(rating),Toast.LENGTH_LONG).show();
 }
});

 }  
 }

另外,请查看以下链接:

  1. http://www.javatpoint.com/android-rating-bar-example
  2. http://webtutsdepot.com/2011/08/20/android-sdk-tutorial-rating-bar-example/
  3. 希望这有帮助。

答案 1 :(得分:0)

属性isIndicator必须为假。

您可以使用二传手以编程方式执行此操作:

    RatingBar ratingBar = findViewById(R.id.YOURID);
    ratingBar.setIsIndicator(false);

或在xml属性中:

    android:isIndicator="false"

确保获得正确的RatingBar。