如图所示,通过点击星形图标进行评分。看似简单,操作起来却也得细心呢!
下面就看看怎么完成它呢:
一 布局:
这个相对简单,只有一个activity_main.xml:
<LinearLayout 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:background="@drawable/bg"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_medium"
android:text="请您评分:"
tools:context=".MainActivity" />
<RatingBar
android:id="@+id/ratingBar1"
style="@style/AppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="0.5"
android:stepSize="0.5" />
<TextView
android:id="@+id/corse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:padding="@dimen/padding_medium"
android:text="评分结果为:"
tools:context=".MainActivity" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/descrption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="推荐指数:" />
<RatingBar
android:id="@+id/ratingBar2"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="false" />
</LinearLayout>
</LinearLayout>
二 中心文件:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义组件对象
final RatingBar rb1=(RatingBar) findViewById(R.id.ratingBar1);
final RatingBar rb2=(RatingBar) findViewById(R.id.ratingBar2);
rb1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// corse.setText(rating);
rb1.setRating(rating);
rb2.setRating(4);
Toast.makeText(MainActivity.this, "评分结果是:"+String.valueOf(rating)+"分", Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}