想做一个评价功能 有星级评价 想问的就是 选中五星 怎么让他变成数字 往接口里传
谁有这方面的例子 谢谢了!
1、MainActivity.java
public class MainActivity extends Activity {
private RatingBar mRatingBar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRatingBar = (RatingBar) findViewById(R.id.ratingbar);
mRatingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(MainActivity.this, "评价了" + rating + "星", Toast.LENGTH_SHORT).show();
}
});
}
}
2、activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="0"
android:stepSize="1" />
</RelativeLayout>
安卓中有rating bar控件
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="320dp"
android:contentDescription="@string/hello"
android:src="@drawable/lijiang" />
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="255"
android:numStars="5"
android:progress="255"
android:stepSize="0.5" />
用OnRatingBarChangeListener来监听评分条的改变。
[java] view plaincopyprint?
public class RatingBarTest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView image = (ImageView)findViewById(R.id.image);
RatingBar ratingBar = (RatingBar)findViewById(R.id.rating);
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
{
//当拖动条的滑块位置发生改变时触发该方法
@Override
public void onRatingChanged(RatingBar arg0
, float rating, boolean fromUser)
{
//动态改变图片的透明度,其中255是星级评分条的最大值,
//5个星星就代表最大值255
image.setAlpha((int)(rating * 255 / 5));
}
});
}
}