Android开发之星级评分条-RatingBar

先上图:

Android开发之星级评分条-RatingBar_第1张图片

布局文件:


主类:
public class MainActivity extends Activity {
	
	private RatingBar rb;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		rb=(RatingBar) findViewById(R.id.rb);
		//设置监听器
		rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			
			@Override
			//如果星级发生变化,就会调用此方法
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				switch((int)rating){
				case 1:
					Toast.makeText(getApplicationContext(), "one star", 0).show();
					break;
				case 2:
					Toast.makeText(getApplicationContext(), "two star", 0).show();
					break;
				case 3:
					Toast.makeText(getApplicationContext(), "three star", 0).show();
					break;
				case 4:
					Toast.makeText(getApplicationContext(), "four star", 0).show();
					break;
				case 5:
					Toast.makeText(getApplicationContext(), "five star", 0).show();
					break;
				}
			}
		});
	}

}



你可能感兴趣的:(Android中级)