how to get RatingBar value

前端 未结 9 2381
傲寒
傲寒 2021-02-13 18:55

how to read and display rating bar value

i[0] // should be selected value

private OnClickListener onclickbutton1 = new OnClickListener() {
    public void onCli         


        
相关标签:
9条回答
  • 2021-02-13 19:26

    Simple call

      ratingBar.getRating() ; GET RATING BAR VALUE
    

    When you want to get the rating value when changed

         public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
    
         final int numStars = ratingBar.getNumStars();
         ratingBar.getRating() ;
         final float ratingBarStepSize = ratingBar.getStepSize();
    
    }
    
    0 讨论(0)
  • 2021-02-13 19:26

    use this to get rating

    (RatingBar)findViewById(R.id.ratingbar1)).setOnRatingBarChangeListener(this);
            ((RatingBar)findViewById(R.id.ratingbar2)).setOnRatingBarChangeListener(this);
        }
    
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
            final int numStars = ratingBar.getNumStars();
            mRatingText.setText( 
                    getString(R.string.ratingbar_rating) + " " + rating + "/" + numStars);
    
     }
    
    0 讨论(0)
  • 2021-02-13 19:33
    RatingBar ratingBar = (RatingBar) findViewById(R.id.mRatingBar);
    RatingBar.setOnClickListener(onclickbutton1);
    
    public void onClick(View v) {
        RatingBar bar = (RatingBar) v;
        rating.setText("value is " + bar.getRating());
    }
    
    0 讨论(0)
  • 2021-02-13 19:37
    var ratingBar = findViewById<RatingBar>(R.id.ratingBarid);
    var text1 = findViewById<TextView>(R.id.textViewid)
    
    // rating bar click to show the text view
    
    ratingBar.setOnRatingBarChangeListener(object: RatingBar.OnRatingBarChangeListener{
    
        override fun onRatingChanged(ratingBar: RatingBar?, rating: Float, fromUser: Boolean) {
           // TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            text1.text = "Rating bar result $rating"
        }
    })
    
    0 讨论(0)
  • 2021-02-13 19:37
        var ratingBar = findViewById<RatingBar>(R.id.ratingBarid);
        var btn= findViewById<Button>(R.id.buttonid);
        var text1 = findViewById<TextView>(R.id.textViewid)
    
    
        /*
    
         3rd rules of
    
        if (ratingBar!=null){
    
            btn.setOnClickListener {
    
                val msg = ratingBar.rating.toString();
                text1.setText("The result of $msg")
            }
        }
    
    
       // rating bar click to show the text view
    
       // ratingBar.setOnRatingBarChangeListener(this)
    
    
    
    
    
    
                  // 1st rules of Rating Bar..
      ratingBar.setOnRatingBarChangeListener(object: RatingBar.OnRatingBarChangeListener{
    
          override fun onRatingChanged(ratingBar: RatingBar?, rating: Float, fromUser: Boolean) {
             // TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    
              text1.text = "Rating bar result $rating"
          }
      })
    
    
    
    
    }
    
    
                // 2nd rules of Rating bar ....
    override fun onRatingChanged(ratingBar: RatingBar?, rating: Float, fromUser: Boolean) {
        //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    
        var text1 = findViewById<TextView>(R.id.textViewid)
        text1.text = "Rating Bar Result : $rating"
    
    
    
        */
    }
    
    0 讨论(0)
  • 2021-02-13 19:43

    What you do here is not right: you output the resource id of the rating bar, not its value.

    Let me assume that you have earlier done something like:

    RatingBar mBar = (RatingBar) findViewById(R.id.mRatingBar);
    mBar.setOnClickListener(onclickbutton1);
    

    for example in the activity's onCreate(). Then within the on click listener you provide, you can get the rating as follows:

    public void onClick(View v) {
        RatingBar bar = (RatingBar) v;
        statusMessage.setText("value is " + bar.getRating());
    }
    
    0 讨论(0)
提交回复
热议问题