Android星級評分條的實現(xiàn)代碼
RatingBar星級評分條
它跟拖動條類似。都允許用戶拖動來改變進度,不同的是,星級評分條通過星星圖案表示進度。想淘寶,等等都有這個東西。很簡單。
重要記一個組件<RatingBar 屬性列表></RatingBar>,屬性見名知意
還有三個方法:
1、getRating()方法:獲取等級,表示你當前選中了幾顆星星
2、getStepSize()方法:每次最少可以改變多少星星(默認是0.5個)
3、getProgress()方法:獲取進度,是前兩個之積。
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():用語獲取進度,獲取到的進度值為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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android IntentService實現(xiàn)原理及內(nèi)部代碼分享
android IntentService實現(xiàn)原理及內(nèi)部代碼分享,需要的朋友可以參考一下2013-06-06
android中可以通過兩種方式調(diào)用接口發(fā)送短信
調(diào)用系統(tǒng)短信接口直接發(fā)送短信;調(diào)起系統(tǒng)發(fā)短信功能,本文將給出兩種方式的實現(xiàn)代碼,感興趣的朋友可以了解下,或許對你有所幫助2013-02-02
Android 實現(xiàn)帶角標的ImageView(微博,QQ消息提示)
下面小編就為大家分享一篇Android 實現(xiàn)帶角標的ImageView(微博,QQ消息提示),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android獲取手機屏幕寬高、狀態(tài)欄高度以及字符串寬高信息的方法
這篇文章主要介紹了Android獲取手機屏幕寬高、狀態(tài)欄高度以及字符串寬高信息的方法,涉及Android獲取文字寬高、狀態(tài)欄高度、textView寬度及屏幕尺寸的相關(guān)技巧,需要的朋友可以參考下2015-04-04
Android webview如何加載HTML,CSS等語言的示例
本篇文章主要介紹了Android webview如何加載HTML,CSS等語言的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Flutter通過Container實現(xiàn)時間軸效果
時間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過Container實現(xiàn),感興趣的朋友可以了解下2021-05-05
Android實現(xiàn)App中導航Tab欄懸浮的功能
相信大家在玩手機的過程中應該會注意到很多的app都有這種功能,比如說外賣達人常用的“餓了么”。所以這篇文章給大家分享了Android如何實現(xiàn)app中的導航Tab欄懸浮的功能,有需要的朋友們可以參考借鑒。2016-10-10

