欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android之RatingBar控件用法详解

时间:2021-10-26 09:12:50|栏目:Android代码|点击:

MainActivity.java

package com.example.mars_2500_ratingbar;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.os.Build;

public class MainActivity extends Activity
{

  RatingBar ratingBar;
  Button button1;
  public static String TAG="MainActivity";
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ratingBar=(RatingBar)findViewById(R.id.ratingBar1);
    ratingBar.setOnRatingBarChangeListener(new RatingBarChangeListener());
    button1=(Button)findViewById(R.id.buttn1);
    button1.setOnClickListener(new ClickListener());
    
  }

  class RatingBarChangeListener implements OnRatingBarChangeListener
  {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating,
        boolean fromUser)
    {
      Log.i(TAG, "当前分数="+rating);
      System.out.println("当前分数="+rating);
    }
  }
  
  class ClickListener implements OnClickListener
  {
    @Override
    public void onClick(View v)
    {  //在当前加0.5分
      ratingBar.setRating(ratingBar.getRating()+0.5f);
    }
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

}

activity_main.xml

<?xml version="1.0" encoding="UTF-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
 
<RatingBar 
    android:id="@+id/ratingBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="0.5"
    />
  <Button 
    android:id="@+id/buttn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ratingBar1"
    android:text="评分"
     />
 </LinearLayout>

上一篇:Android中Notification用法实例总结

栏    目:Android代码

下一篇:Android-自定义控件之ListView下拉刷新的实现

本文标题:android之RatingBar控件用法详解

本文地址:http://www.codeinn.net/misctech/181351.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有