效果图:
直接上代码:
xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.blogtest.MainActivity"> <!--numStars设置星星的数量,stepSize默认的评分--> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_main_rating" android:numStars="5" android:stepSize="0.5" /> </LinearLayout>
Java代码:
package com.example.blogtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.widget.RatingBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private RatingBar rb_main_rating; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得RatingBar的控件 rb_main_rating = (RatingBar) findViewById(R.id.rb_main_rating); //给控件设置监听事件 rb_main_rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Toast t=Toast.makeText(MainActivity.this,"您的评分为:"+rating,Toast.LENGTH_SHORT); t.setGravity(Gravity.CENTER,0,0); t.show(); } }); } }
以上所述是小编给大家介绍的Android RatingBar 评分控件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对找一找教程网网站的支持!