Android 自定义RatingBar评分控件

1、xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RatingBar
        android:id="@+id/describe_rb"
        style="@style/Custom_commont_RatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:rating="0"
        android:stepSize="1.0" />

    <TextView
        android:id="@+id/describe_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dip"
        android:padding="20dip"
        android:text="值"
        android:textSize="50sp" />

    <Button
        android:id="@+id/exit_login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="确定"
        android:textColor="#ffffff"
        android:textSize="16sp"
        android:textStyle="bold" />

</LinearLayout>

2、activity

package com.evaluation.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	private RatingBar describe_rb;// 描述
	private TextView describe_tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_evaluation);
		getXmlView();
	}

	private void getXmlView() {
		describe_rb = (RatingBar) findViewById(R.id.describe_rb);
		describe_tv = (TextView) findViewById(R.id.describe_tv);
		this.findViewById(R.id.exit_login).setOnClickListener(new onclick());
		describe_rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
				final float score = describe_rb.getRating();
				String ss = String.format("%1.0f", score);
				describe_tv.setText(ss + "分");
			}
		});
	}

	class onclick implements OnClickListener {
		@Override
		public void onClick(View v) {
			final float score = describe_rb.getRating();
			if (score == 0) {
				Toast.makeText(MainActivity.this, "请评分", Toast.LENGTH_LONG).show();
				return;
			}
			String ss = String.format("%1.0f", score);
			Toast.makeText(MainActivity.this, ss, Toast.LENGTH_SHORT).show();
		}

	}

}

3、style

  <style name="Custom_commont_RatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/ratingbar_add</item>
        <item name="android:minHeight">30dip</item>
        <item name="android:maxHeight">40dip</item>
    </style>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值