评分栏,如何获得星数?

Rating bar, how to get the number of stars?

我的程序中有一个编辑文本和一个评级栏。我正在检查用户在编辑文本中是否有超过 3 个字符,对于评级栏,它应该有大于 0.5 星的东西(基本上是为了检查他们是否至少点击了其中的任何一个)。

    final EditText commentbox = (EditText)findViewById(R.id.comment);
    final RatingBar rating = (RatingBar)findViewById(R.id.rating);
    final Button feedbackbutton = (Button)findViewById(R.id.submit);


    final TextWatcher watcher = new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            if(commentbox.length() >= 3 && rating.getNumStars() >=0.5){
                feedbackbutton.setEnabled(true);
                feedbackbutton.setBackgroundColor(Color.parseColor("#369742"));
            } else  {
                feedbackbutton.setEnabled(false);
                feedbackbutton.setBackgroundColor(Color.parseColor("#ffcacaca"));
            }


        }

    };
    commentbox.addTextChangedListener(watcher);
    rating.setOnRatingBarChangeListener((this));

如您所见,它必须满足这些条件才能启用按钮并更改其背景颜色。但是,只要我在编辑文本中写入超过 3 个字符,该按钮就会自动启用并改变其颜色。它不是在寻找评级栏条件。

有人可以帮我吗

 feedbackbutton.setOnClickListener(new OnClickListener(){  

            @Override  
            public void onClick(View arg0) {  
                //Getting the rating and displaying it on the toast  
                String rating=String.valueOf(rating.getRating());  
                Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();  
            }  

        });

希望它能解决您的问题。

根据评分值获取评分:

float ratingValue =  rating.getRating();

添加侦听器:

rating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

            @Override
            public void onRatingChanged(RatingBar arg0, float rateValue, boolean arg2) {
                // TODO Auto-generated method stub
                Log.d("Rating", "your selected value is :"+rateValue);
            }
        });