今天来实现一个自定义的RatingBar

//首先在你需要用到的地方引用这个类

 <com.fans.momhelpers.widget.RattingStar
            android:id="@+id/rating_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/w25"
            android:layout_marginLeft="@dimen/w20"
/>

//RattingStar这个类中代码的实现

public class RattingStar extends LinearLayout implements OnClickListener {

private CheckBox star1;
private CheckBox star2;
private CheckBox star3;
private CheckBox star4;
private CheckBox star5;

@SuppressLint("NewApi")
public RattingStar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public RattingStar(Context context, AttributeSet attrs) {

super(context, attrs);
init();
}
public RattingStar(Context context) {
super(context);
init();
}
private void init() {
inflate(getContext(), R.layout.ratting_star, this);
star1 = (CheckBox) findViewById(R.id.star1);
star2 = (CheckBox) findViewById(R.id.star2);
star3 = (CheckBox) findViewById(R.id.star3);
star4 = (CheckBox) findViewById(R.id.star4);
star5 = (CheckBox) findViewById(R.id.star5);
star1.setOnClickListener(this);
star2.setOnClickListener(this);
star3.setOnClickListener(this);
star4.setOnClickListener(this);
star5.setOnClickListener(this);
}
/**
* 是否可以选择
* 
* @param  checkable
*/
public void setCheckable(boolean checkable) {
star1.setEnabled(checkable);
star2.setEnabled(checkable);
star3.setEnabled(checkable);
star4.setEnabled(checkable);
star5.setEnabled(checkable);
}
@Override
public void onClick(View v) {
star1.setChecked(false);
star2.setChecked(false);
star3.setChecked(false);
star4.setChecked(false);
star5.setChecked(false);
switch (v.getId()) {
case R.id.star1:
score = 1;
star1.setChecked(true);
break;
case R.id.star2:
score = 2;
star1.setChecked(true);
star2.setChecked(true);
break;
case R.id.star3:
score = 3;
star1.setChecked(true);
star2.setChecked(true);
star3.setChecked(true);
break;
case R.id.star4:
score = 4;
star1.setChecked(true);
star2.setChecked(true);
star3.setChecked(true);
star4.setChecked(true);
break;
case R.id.star5:
score = 5;
star1.setChecked(true);
star2.setChecked(true);
star3.setChecked(true);
star4.setChecked(true);
star5.setChecked(true);
break;
default:
break;
}
if (onScoreChangedListener != null)
onScoreChangedListener.onScoreChanged(score);
}

private int score = 0;

public int getScore() {
return score;
}

/**
* 设置分数,1-5分
* 
* @param score
*/
public void setScore(int score) {
this.score = score;
star1.setChecked(false);
star2.setChecked(false);
star3.setChecked(false);
star4.setChecked(false);
star5.setChecked(false);
if (score >= 1) {
star1.setChecked(true);
}
if (score >= 2) {
star2.setChecked(true);
}
if (score >= 3) {
star3.setChecked(true);
}
if (score >= 4) {
star4.setChecked(true);
}
if (score >= 5) {
star5.setChecked(true);
}
}

private OnScoreChangedListener onScoreChangedListener;

public OnScoreChangedListener getOnScoreChangedListener() {
return onScoreChangedListener;
}

public void setOnScoreChangedListener(OnScoreChangedListener onScoreChangedListener) {
this.onScoreChangedListener = onScoreChangedListener;
}

public interface OnScoreChangedListener {

public void onScoreChanged(int score);
}
}


//接下来定义RattingStar这个类的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="@dimen/w30"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/star1"
        android:layout_width="@dimen/w30"
        android:layout_height="@dimen/w30"
        android:background="@drawable/ic_rating_star"
        android:button="@null"
         />

    <CheckBox
        android:id="@+id/star2"
        android:layout_width="@dimen/w30"
        android:layout_height="@dimen/w30"
        android:layout_marginLeft="@dimen/w10"
        android:background="@drawable/ic_rating_star"
        android:button="@null" />

    <CheckBox
        android:id="@+id/star3"
        android:layout_width="@dimen/w30"
        android:layout_height="@dimen/w30"
        android:layout_marginLeft="@dimen/w10"
        android:background="@drawable/ic_rating_star"
        android:button="@null" />

    <CheckBox
        android:id="@+id/star4"
        android:layout_width="@dimen/w30"
        android:layout_height="@dimen/w30"
        android:layout_marginLeft="@dimen/w10"
        android:background="@drawable/ic_rating_star"
        android:button="@null" />

    <CheckBox
        android:id="@+id/star5"
        android:layout_width="@dimen/w30"
        android:layout_height="@dimen/w30"
        android:layout_marginLeft="@dimen/w10"
        android:background="@drawable/ic_rating_star"
        android:button="@null" />

</LinearLayout>

//接下来定义CheackBox的background  就是两张小星的图片一张是选中一张是没有选中的

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/icon_star"/>
    <item android:drawable="@drawable/icon_star_empty"/>
</selector>





转载于:https://my.oschina.net/androidGreenhand/blog/519363

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值