lovejobs

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  138 随笔 :: 3 文章 :: 46 评论 :: 28万 阅读

1.设置布局文件,自定义ratingbar样式

复制代码
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="${relativePackage}.${activityClass}" >
 6 
 7 
 8     <RatingBar
 9         android:id="@+id/rb"
10         android:layout_width="wrap_content"
11         android:layout_height="30dp"
12         android:layout_marginLeft="20dp"
13         android:layout_marginTop="20dp"
14         android:numStars="5"
15         android:progressDrawable="@drawable/rating_star"
16         android:stepSize="0.5" /> <!-- 自定义ratingbar样式 -->
17 
18     <TextView
19         android:id="@+id/tv_num"
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:layout_below="@+id/rb"
23         android:layout_marginLeft="20dp"
24         android:layout_marginTop="20dp"
25         android:text="当前评分是:" />
26 
27     <TextView
28         android:id="@+id/tv"
29         android:layout_width="wrap_content"
30         android:layout_height="wrap_content"
31         android:layout_below="@+id/rb"
32         android:layout_marginTop="20dp"
33         android:layout_toRightOf="@id/tv_num"
34         android:text="0.0"
35         android:textColor="#FF0000" />
36 
37 </RelativeLayout>
复制代码

2.在activity中设置监听器

复制代码
 1 package com.example.ratingbar;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Menu;
 6 import android.view.MenuItem;
 7 import android.view.View;
 8 import android.widget.RatingBar;
 9 import android.widget.RatingBar.OnRatingBarChangeListener;
10 import android.widget.TextView;
11 
12 public class MainActivity extends Activity {
13 
14     private TextView tv;
15     private RatingBar rb;
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         //找到控件
22         tv = (TextView) findViewById(R.id.tv);
23         rb = (RatingBar) findViewById(R.id.rb);
24         rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {//设置ratingbar监听器
25             
26             @Override
27             public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
28                 if(fromUser){//如果是用户操作
29                     tv.setText(Float.toString(rating));//显示分数
30                 }
31                 
32             }
33         });
34         
35     }
36 }
复制代码

3.效果图

posted on   lovejobs  阅读(2142)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示