Android RatingBar星星评分控件实例代码

2022-10-19,,

本文通过实例代码给大家介绍了Android RatingBar星星评分控件,非常不错,具有参考借鉴价值,需要的朋友参考下吧

效果图:

直接上代码:

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 评分控件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对北冥有鱼网站的支持!

您可能感兴趣的文章:

  • Android开发之自定义星星评分控件RatingBar用法示例
  • Android星级评分条控件RatingBar使用详解
  • Android自定义控件RatingBar调整字体大小
  • Android开发手册RatingBar星级评分控件实例

《Android RatingBar星星评分控件实例代码.doc》

下载本文的Word格式文档,以方便收藏与打印。