【问题标题】:Rating bar, how to get the number of stars?评分栏,如何获得星数?
【发布时间】:2015-03-26 10:43:54
【问题描述】:

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

谁能帮帮我

【问题讨论】:

    标签: java android xml textwatcher ratingbar


    【解决方案1】:

    从评分值获得评分:

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

    【讨论】:

    • 完美。谢谢!
    【解决方案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();  
                }  
    
            });
    

    希望它能解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 2018-03-21
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多