Android星級評分條的實(shí)現(xiàn)代碼
RatingBar星級評分條
它跟拖動條類似。都允許用戶拖動來改變進(jìn)度,不同的是,星級評分條通過星星圖案表示進(jìn)度。想淘寶,等等都有這個東西。很簡單。
重要記一個組件<RatingBar 屬性列表></RatingBar>,屬性見名知意
還有三個方法:
1、getRating()方法:獲取等級,表示你當(dāng)前選中了幾顆星星
2、getStepSize()方法:每次最少可以改變多少星星(默認(rèn)是0.5個)
3、getProgress()方法:獲取進(jìn)度,是前兩個之積。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.amy.ratingbartest.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp">
<!-- 星級評分條 -->
<RatingBar
android:id="@+id/ratingBar1"
android:numStars="5"
android:rating="2.5"
android:isIndicator="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 按鈕 -->
<Button
android:id="@+id/button1"
android:text="提交"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.amy.ratingbartest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private RatingBar ratingBar;//星級評分條
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* getRating():用于獲取等級,表示選中的幾顆星
* getStepSize():用語獲取每次至少要改變多少個星級
* getProgress():用語獲取進(jìn)度,獲取到的進(jìn)度值為getRating()方法返回值與getStepSize()方法返回值之積
*/
int result = ratingBar.getProgress();
float rating = ratingBar.getRating();
float step = ratingBar.getStepSize();
Log.e("星級評分條","step="+step+"result="+result+"rating="+rating);
Toast.makeText(MainActivity.this,"你得到了"+rating+"顆星",Toast.LENGTH_SHORT).show();
}
});
}
}
效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Volley擴(kuò)展實(shí)現(xiàn)支持進(jìn)度條的文件上傳功能
這篇文章主要為大家詳細(xì)介紹了Android Volley擴(kuò)展實(shí)現(xiàn)文件上傳與下載功能,支持進(jìn)度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
android通過okhttpClient下載網(wǎng)頁內(nèi)容的實(shí)例代碼
本篇文章主要介紹了android通過okhttpClient下載網(wǎng)頁內(nèi)容的實(shí)例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android開發(fā)實(shí)現(xiàn)繪制淘寶收益圖折線效果示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)繪制淘寶收益圖折線效果,涉及Android canvas圖形繪制及布局控制相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android編程獲取設(shè)備MAC地址的實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程獲取設(shè)備MAC地址的實(shí)現(xiàn)方法,涉及Android針對硬件設(shè)備的操作技巧,需要的朋友可以參考下2017-01-01
Android 關(guān)于ExpandableListView去掉里頭分割線的方法
下面小編就為大家?guī)硪黄狝ndroid 關(guān)于ExpandableListView去掉里頭分割線的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
從源代碼分析Android Universal ImageLoader的緩存處理機(jī)制
這篇文章主要介紹了從源代碼分析Android Universal ImageLoader的緩存處理機(jī)制 的相關(guān)資料,需要的朋友可以參考下2016-01-01

