Android控件開發之RatingBar

RatingBar是SeekBar和ProgressBar的擴展,用星星來評級。使用的默認大小RatingBar時,用戶可以觸摸/拖動或使用鍵來設置評分


RatingBar效果



評分控件中兩個比較重要的方法:

RatingBar.setRating(flaot rating); 
RatingBar.getRating();

本程序main.xml源碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView  
    android:id="@+id/text"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="這是一個評分控件     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:rating="1.5"
    android:stepSize="0" />
<!-- android:numStars="5"總級別,總分,星星個數 -->
<!--  android:rating="1.5"當前級別,分數,星星個數-->
</LinearLayout>






事件監聽處理

RatingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
{ 
    public void onRatingChanged(RatingBar ratingBar, float rating,  boolean fromUser) 

    { 
           //doing actions 
    } 
});



本程序java源碼

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;

public class RatingBarActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final RatingBar ratingbar = (RatingBar)findViewById(R.id.ratingbar);
        final TextView text = (TextView)findViewById(R.id.text);
        
        ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() 
        {
		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) 
		{
			text.setText("Rating:" + rating);

		}
	});
    }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章