activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RatingBar android:id="@+id/rating_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="3"
android:rating="2.5">
</RatingBar>
<RatingBar android:id="@+id/rating_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="2.25">
</RatingBar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip">
<TextView
android:id="@+id/rating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RatingBar android:id="@+id/small_ratingbar"
style="?android:attr/ratingBarStyleSmall"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<RatingBar android:id="@+id/indicator_ratingbar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements RatingBar.OnRatingBarChangeListener
{
RatingBar mSmallRatingBar;
RatingBar mIndicatingRatingBar;
TextView mRatingText;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRatingText=(TextView)findViewById(R.id.rating);
mSmallRatingBar=(RatingBar)findViewById(R.id.small_ratingbar);
mIndicatingRatingBar=(RatingBar)findViewById(R.id.indicator_ratingbar);
((RatingBar)findViewById(R.id.rating_1)).setOnRatingBarChangeListener(this);
((RatingBar)findViewById(R.id.rating_2)).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);
if(mIndicatingRatingBar.getNumStars()!=numStars)
{
mIndicatingRatingBar.setNumStars(numStars);
mSmallRatingBar.setNumStars(numStars);
}
if(mIndicatingRatingBar.getRating()!=rating)
{
mIndicatingRatingBar.setRating(rating);
mSmallRatingBar.setRating(rating);
}
final float ratingBarStepSize=ratingBar.getStepSize();
if(mIndicatingRatingBar.getStepSize()!=ratingBarStepSize)
{
mIndicatingRatingBar.setStepSize(ratingBarStepSize);
mSmallRatingBar.setStepSize(ratingBarStepSize);
}
}
}
this good info!
ReplyDelete