为什么评级栏的onRatingBarChangeListner不起作用?

时间:2016-09-09 17:10:02

标签: android ratingbar

我正在尝试在代码中添加curr_sql。我正在使用rec1来获取评级栏的值。但rating bar根本没有回应。它不会被选中或改变颜色。

onRatingBarChangeListner

试过这个,但是没有用。

rating bar

还尝试了这个tutorial

出了什么问题?有人可以帮忙吗?谢谢..

编辑:尝试了两个答案仍然无效:

public class SurveyActivity extends AppCompatActivity implements getQuestionsAsyncTask.GetQuestions,RatingBar.OnRatingBarChangeListener{

private ArrayList<Question> mQuestionsArrayList;
private TextView mTextQuestion1,mTextQuestion2;
private RatingBar ratingbar1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_survey);

 //   addListenerOnButtonClick();

    mTextQuestion1 = (TextView)findViewById(R.id.textQuestion1);
    mTextQuestion2 = (TextView)findViewById(R.id.textQuestion2);

    Button next = (Button)findViewById(R.id.btn_next);

    final RatingBar ratingBar = (RatingBar)findViewById(R.id.MyRating);

   next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            float rating=ratingBar.getRating();
            Toast.makeText(getApplicationContext(),"Your Selected Ratings  : " + String.valueOf(rating),Toast.LENGTH_LONG).show();

         //   startActivity(new Intent(SurveyActivity.this,UserDetailsActivity.class));
        }
    });



    mQuestionsArrayList = new ArrayList<>();

    getQuestionsAsyncTask getQuestionsAsyncTask = new getQuestionsAsyncTask(this,this,mQuestionsArrayList);
    getQuestionsAsyncTask.execute();
}
public void onRatingChanged(RatingBar ratingBar, float rating,
                            boolean fromUser) {

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

}

2 个答案:

答案 0 :(得分:1)

您的onRatingChanged应如下

@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {

}

同时在setOnRatingBarChangeListener

上添加ratingBar
ratingBar.setOnRatingBarChangeListener(this);

编辑1 :对于Toast,请尝试按照

进行操作
String rating=String.valueOf(ratingBar.getRating());  
Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();  

编辑2 :由于isIndicator属性,您无法更改评分。根据android的文档

  

android:isIndicator此评级栏是否为指标(和   用户不可更改)。

删除该属性并将ratingBar更改为以下

<RatingBar
        android:id="@+id/MyRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="0.1"
        android:layout_above="@+id/textQuestion2"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_alignParentRight="true" />

也为什么我删除了行

style="?android:attr/ratingBarStyleSmall"

参考this

答案 1 :(得分:1)

您忘记在ratingBar视图中设置更改侦听器

final RatingBar ratingBar = (RatingBar)findViewById(R.id.MyRating);
ratingBar.setOnRatingBarChangeListener(this);