• 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

    <?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>
  • 相关阅读:
    poj----1330Nearest Common Ancestors(简单LCA)
    Tarjan--LCA算法的个人理解即模板
    hdu----(4545)魔法串(LCS)
    hdu---(1325)Is It A Tree?(并查集)
    hdu----(1599)最大子矩阵(几何/dp)
    hdu---(1054)Strategic Game(最小覆盖边)
    整理的一些模版LCS(连续和非连续)
    hdu---(4310)Hero(贪心算法)
    hdu----(4308)Saving Princess claire_(搜索)
    hdu------(4302)Holedox Eating(树状数组+二分)
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3675381.html
Copyright © 2020-2023  润新知