android之RatingBar控件用法详解

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


 
 

  

以上就是小编为大家带来的android之RatingBar控件用法详解的全部内容了,希望对大家有所帮助,多多支持脚本之家~

你可能感兴趣的:(android之RatingBar控件用法详解)