RatingBar -- 用于淘宝那种五角星评分android
//res/layout 布局界面有 一个默认的RatingBar
//一个自定义的RatingBar 和一个 俺就 控件
一、在res/layout 布局 界面 布局控件app
代码ide
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >布局
<LinearLayout
android:id="@+id/one_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal" >this
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="评分:"
android:textSize="25sp" />xml
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>图片
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/one_linear"
android:layout_centerInParent="true"
android:text="提交"
android:textSize="40sp"
android:onClick="onclick"/>get
<LinearLayout
android:id="@+id/two_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/submit"
android:orientation="horizontal" >it
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结果:"
android:textSize="25sp" />
<!-- 设置numStars = 5 表示 自定义 星星的个数 -->
<RatingBar
android:id="@+id/ratingbar_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:progressDrawable="@drawable/my_image" />
</LinearLayout>io
</RelativeLayout>
---------------------------
二、在res/drawable-hdpi导入五角星图片
//一个没颜色的(等待评分的五角星)
//另一个是有颜色的 -- 表示评分后的 五角星
-----------------------------
三、在MainActivity 实现评分的功能代码
代码
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {private RatingBar ratingbar;private RatingBar ratingbar_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ratingbar = (RatingBar) this.findViewById(R.id.ratingbar); ratingbar_result = (RatingBar) this.findViewById(R.id.ratingbar_result); /*ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) { // TODO Auto-generated method stub } });*/ } public void onclick(View view){//得到 当前的评分 float f = ratingbar.getRating(); Toast.makeText(this, "评分结果:" + f, Toast.LENGTH_SHORT).show();//设置结果评分 ratingbar_result.setRating(f); }}