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>
相關文章
相關標籤/搜索