MainActivity.java:
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.RatingBar; import android.widget.Toast; import android.widget.RatingBar.OnRatingBarChangeListener; public class MainActivity extends Activity implements OnRatingBarChangeListener { private RatingBar ratingBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ratingBar = (RatingBar) findViewById(R.id.ratingbar); ratingBar.setMax(100);// 设置最大刻度 ratingBar.setProgress(20);// 设置当前刻度 ratingBar.setOnRatingBarChangeListener(this); } @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // TODO Auto-generated method stub int progress = ratingBar.getProgress(); Toast.makeText(MainActivity.this, "progress:" + progress + " rating:" + rating + "是否是用户触发:" + fromUser, 0).show(); } }
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.ratingbardemo.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="评分控件的使用" android:textSize="20dp" /> <RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
原文地址:http://blog.csdn.net/u013476556/article/details/45289305