评分栏,如何获得星数?

时间:2015-03-26 10:43:55

标签: java android xml textwatcher ratingbar

我的程序中有一个编辑文本和一个评级栏。我正在检查用户在编辑文本中是否有超过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个字符时,该按钮会自动启用并更改其颜色。它不是在寻找评级栏条件。

有人可以帮助我吗

2 个答案:

答案 0 :(得分:2)

 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();  
            }  

        });

希望它能解决你的问题。

答案 1 :(得分:2)

要从评级值获得评分:

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);
            }
        });
相关问题