开发者

how to get RatingBar value

how to read and display rating bar value

i[0] // should be selected value

private OnClickListener onclickbutton1 = new OnClickListener() {
    public void onClick(View v) {
        int[] i = new int[]{ R.id.mRatingBar};

        statusMessage.setText("value is " + i[0]);
    }
};

// this works

private OnClickListene开发者_JAVA百科r onclickbutton1 = new OnClickListener() {
    public void onClick(View v) {
        RatingBar mBar = (RatingBar) findViewById(R.id.mRatingBar);

        float[] i = new float[]{ mBar.getRating() };

        statusMessage.setText("value is.. " + i[0]);
    }
};


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


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

}


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

 }


rbRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

                Float ratingVal = (Float) rating;
                Float ratingvalue = (Float) rbRatingBar.getRating();
                Toast.makeText(getApplicationContext(), " Ratings : " + String.valueOf(ratingVal) + "", Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), " Ratings1 : " + ratingvalue + "", Toast.LENGTH_SHORT).show();
            }
        });


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


RatingBar rate = (RatingBar)findViewById(R.id.R_rate);

//use ratings within event listner code block

float rating = rate.getRating();


import android.widget.RatingBar;

public class PageAboutFragment extends Fragment implements RatingBar.OnRatingBarChangeListener  {


rbSearch.setOnRatingBarChangeListener(this);


public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        tvRatingContent.setText(rating+"/5 Stars");
}


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


    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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜