代码改变世界

RatingBar评分控件的使用

2014-04-01 20:21  kingshow  阅读(203)  评论(0编辑  收藏  举报

 

    RatingBar主要用在电子相册,网上书店和对文章进行评分的功能。

一、建立工程,如图

二、activity_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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="评分控件的使用"
        />
    <RatingBar 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ratingBar"
        />
    
</LinearLayout>
View Code

三、MainActivity.java中代码

package com.study.ratingbar;

import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.view.Menu;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnRatingBarChangeListener{

    private RatingBar ratingBar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ratingBar = (RatingBar)this.findViewById(R.id.ratingBar);
        ratingBar.setMax(100); //设置最大刻度
        ratingBar.setProgress(20); //设置当前的刻度
        ratingBar.setOnRatingBarChangeListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating,
            boolean fromUser) {
        int progress = ratingBar.getProgress();
        Toast.makeText(MainActivity.this, "progress:" + progress +" rating:"  + rating, 1).show();
        
    }
    
}
View Code

四、效果图