星星评价控件android开发_RatingBar Android星星评分控件

简述:

一个的自定义的Android评分控件(极简洁,易用)!

可更改图标,大小,间距,可选半图模式,点击模式,拖动模式,另外有回调监听。

实现:(直接ctrl +c,v)

在project 的 build.gradle 下:(studio现在会默认实现)

allprojects {

repositories {

jcenter()

}

}

在module 的 build.gradle 下:

compile 'com.xl.ratingbar:ratingbar:0.1.1'

在activity_main XML中:

xmlns:tools="http://schemas.android.com/tools"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:gravity="center_horizontal"

tools:context="com.xl.ratingbar.MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="80dp"

android:textSize="15sp"

android:textColor="#4D4D4D"

android:text="@string/bar1_name"

/>

android:id="@+id/bar1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="8dp"

app:clickable="true"

app:starCount="5"

app:starNum="1"

app:halfstart="true"

app:starEmpty="@mipmap/star_empty"

app:starHalf="@mipmap/star_half"

app:starFill="@mipmap/star_full"

app:starImageHeight="30dp"

app:starImageWidth="30dp"

app:starImagePadding="15dp"

>

android:id="@+id/text1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="8dp"

android:textSize="15sp"

android:textColor="#4D4D4D"

android:text="@string/score"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="30dp"

android:textSize="15sp"

android:textColor="#4D4D4D"

android:text="@string/bar2_name"

/>

android:id="@+id/bar2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="8dp"

app:touchable="true"

app:clickable="false"

app:starCount="5"

app:starNum="2"

app:halfstart="true"

app:starEmpty="@mipmap/fat_star_empty"

app:starHalf="@mipmap/fat_star_half"

app:starFill="@mipmap/fat_star_full"

app:starImageHeight="30dp"

app:starImageWidth="30dp"

app:starImagePadding="15dp"

>

android:id="@+id/text2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="8dp"

android:textSize="15sp"

android:textColor="#4D4D4D"

android:text="@string/score"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="30dp"

android:textSize="15sp"

android:textColor="#4D4D4D"

android:text="@string/bar3_name"

/>

android:id="@+id/bar3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="8dp"

app:touchable="true"

app:starCount="5"

app:starNum="3"

app:halfstart="false"

app:starEmpty="@mipmap/amimia"

app:starFill="@mipmap/smiling"

app:starImageHeight="30dp"

app:starImageWidth="30dp"

app:starImagePadding="15dp"

>

android:id="@+id/text3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="8dp"

android:textSize="15sp"

android:textColor="#4D4D4D"

android:text="@string/comment"

/>

MainActivity :

package com.xl.ratingbar;

import android.graphics.drawable.Drawable;

import android.support.v4.content.ContextCompat;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements MyRatingBar.OnRatingChangeListener {

private TextView text1;

private TextView text2;

private TextView text3;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

text1 = (TextView)findViewById(R.id.text1);

text2 = (TextView)findViewById(R.id.text2);

text3 = (TextView)findViewById(R.id.text3);

MyRatingBar bar1 = (MyRatingBar)findViewById(R.id.bar1);

MyRatingBar bar2 = (MyRatingBar)findViewById(R.id.bar2);

MyRatingBar bar3 = (MyRatingBar)findViewById(R.id.bar3);

bar1.setOnRatingChangeListener(this);

bar2.setOnRatingChangeListener(this);

bar3.setOnRatingChangeListener(this);

}

@Override

public void onRatingChange(MyRatingBar bar, float RatingCount) {

switch (bar.getId()) {

case R.id.bar1:

text1.setText(getResources().getString(R.string.score).concat(String.valueOf(RatingCount)));

break;

case R.id.bar2:

text2.setText(getResources().getString(R.string.score).concat(String.valueOf(RatingCount)));

break;

case R.id.bar3:

changeByStar(bar,RatingCount,text3);

break;

}

}

private void changeByStar(MyRatingBar bar,float star,TextView textView){

Drawable amimia = ContextCompat.getDrawable(this, R.mipmap.amimia);

Drawable crying = ContextCompat.getDrawable(this, R.mipmap.crying);

Drawable smiling = ContextCompat.getDrawable(this, R.mipmap.smiling);

if (star == 1.0f) {

chage(bar, textView, amimia, crying, getResources().getString(R.string.Worst));

}else if (star == 2.0f) {

chage(bar, textView, amimia, crying, getResources().getString(R.string.Bad));

}else if (star == 3.0f) {

chage(bar, textView, amimia, smiling, getResources().getString(R.string.Good));

}else if (star == 4.0f) {

chage(bar, textView, amimia, smiling, getResources().getString(R.string.Better));

}else if (star == 5.0f) {

chage(bar, textView, amimia, smiling, getResources().getString(R.string.Best));

}

}

private void chage(MyRatingBar bar, TextView textView, Drawable empty, Drawable full, String string) {

textView.setText(string);

bar.setStarEmptyDrawable(empty);

bar.setStarFillDrawable(full);

}

}

OK,看效果!(记得 github star一下呦。)

初始化.jpg

选择.jpg

属性:

image.png

注意 :

touchable、clickable 同时为true 时 默认为拖动模式

starHalf 不是半个图,而是一半选择状态一般为选择状态,例:

fat_star_half.jpg

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值