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

//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>


转载于:https://my.oschina.net/u/2541146/blog/603620

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值