Android RatingBar星星评分控件实例代码
程序员文章站
2023-12-09 21:02:21
效果图:
直接上代码:
xml文件:
<...
效果图:
直接上代码:
xml文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout 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.example.blogtest.mainactivity"> <!--numstars设置星星的数量,stepsize默认的评分--> <ratingbar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_main_rating" android:numstars="5" android:stepsize="0.5" /> </linearlayout>
java代码:
package com.example.blogtest; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.gravity; import android.widget.ratingbar; import android.widget.toast; public class mainactivity extends appcompatactivity { private ratingbar rb_main_rating; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //获得ratingbar的控件 rb_main_rating = (ratingbar) findviewbyid(r.id.rb_main_rating); //给控件设置监听事件 rb_main_rating.setonratingbarchangelistener(new ratingbar.onratingbarchangelistener() { @override public void onratingchanged(ratingbar ratingbar, float rating, boolean fromuser) { toast t=toast.maketext(mainactivity.this,"您的评分为:"+rating,toast.length_short); t.setgravity(gravity.center,0,0); t.show(); } }); } }
以上所述是小编给大家介绍的android ratingbar 评分控件,希望对大家有所帮助