Android RatingBar 使用示例

布局文件内容:




    

    


主程序文件内容;

package com.toby.personal.testlistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RatingBar;

public class MainActivity extends AppCompatActivity {

    final private static String TAG = "Toby_Test";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView imageView = (ImageView) findViewById(R.id.imageView);
        final RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                imageView.setImageAlpha((int)(rating * 255 /5));
            }
        });
    }

}

运行效果:


Android RatingBar 使用示例_第1张图片
RatingBar 使用示例

参考文献:《疯狂Android讲义(第2版)》

你可能感兴趣的:(Android RatingBar 使用示例)