Android星级评分条

今天没事写了一个星级评分条,分享给大家

    项目布局:

    android:id="@+id/mRatingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="0.5"/>

numStarts:显示多少个星星,必须为整数

stepSize:评分每次增加的值

RatingBar还有其他的参数:

       isIndicator:是否用作指示,用户无法更改,默认为false      

       rating:默认评分值,必须为浮点数


activity代码:

public class MainActivity extends AppCompatActivity {
    private RatingBar mRatingBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRatingBar = (RatingBar)findViewById(R.id.mRatingBar);
        mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                Toast.makeText(MainActivity.this, "rating:" + String.valueOf(rating),
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}

运行截图:

Android星级评分条_第1张图片


你可能感兴趣的:(Android)