您的位置:首页 > 移动开发 > Android开发

android基本控件示例RatingBar,即五个星星的评分条(03)

2016-01-16 00:00 381 查看
摘要: android基本控件示例RatingBar,即五个星星的评分条

//android基本控件示例RatingBar
public class MainActivity extends Activity {
private RatingBar ratingBar;
private RatingBar rating_result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
rating_result = (RatingBar) findViewById(R.id.rating_result);
// 用户点击评分的评分条监听
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {// fromUser 当用户点击时返回true
Log.i("RatingBar", "当前评分: " + rating + "fromUser: " + fromUser);
}
});
/*
* ratingBar.setOnRatingBarChangeListener(new
* OnRatingBarChangeListener() { public void onRatingChanged(RatingBar
* ratingBar, float rating, boolean fromUser) {//fromUser 当用户点击时返回true
* Log.i("RatingBar", "当前评分: "+rating+"fromUser: "+fromUser); } });
*/
}

public void clickButton(View view) {
float rating = ratingBar.getRating();// 获取评分
rating_result.setRating(rating);// 给结果ratingBar赋值评分
Toast.makeText(MainActivity.this, "结果评分:" + rating, Toast.LENGTH_SHORT)
.show();
}
}
//布局文件
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请评分"
/>
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

<Button
android:id="@+id/btn_finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成评分"
android:onClick="clickButton" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="最后得分"
/>
<!--这个是自定义的RatingBar -->
<RatingBar
android:id="@+id/rating_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:progressDrawable="@drawable/my_rating_drawable"
/>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: