Android UI设计 评分控件RatingBar用法

如果大家去market下软件,在下载完的软件那,每个软件都会有5个五角星让你评分,其实这个就是RatingBar,RatingBar可以方便的让用户帮助你打分。
下面是效果图:

界面上就一个TextView,一个RatingBar,当用户执行评分操作时,TextView上会显示当前用户评分。
下面我们来实现。先是布局xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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/tv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=""
    />
<RatingBar android:id="@+id/rb"
	android:layout_width="wrap_content"
	android:layout_height="fill_parent"
	android:numStars="3"
	android:rating="1"
/>
<!--这里要注意了,layout_width必须是wrap_content,如果设成fill_parent,
不管你在后面把满分设为几颗星,它都会把屏幕横向显示满为止-->
</LinearLayout>

其中,以下两句第一句是用来定义总分,第二句是默认评分,其实这两句在这个程序中没用的,因为等会我们在程序代码中要重定义,这里写上只不过是演示用法。

1
2
android:numStars="3"
android:rating="1"

下面是程序代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.pocketdigi;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.RatingBar.OnRatingBarChangeListener;
 
public class main extends Activity {
    /** Called when the activity is first created. */
	RatingBar rb;
	TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.tv);
        rb=(RatingBar)findViewById(R.id.rb);
        rb.setNumStars(5);
        rb.setRating(3);   
        rb.setOnRatingBarChangeListener(rbLis);
        //定义一个监听器
    }
    private OnRatingBarChangeListener rbLis=new OnRatingBarChangeListener(){
 
		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser) {
			tv.setText(String.valueOf(rb.getRating()));
			//输出评分
		}
 
    };
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值