android之RatingBar控件用法详解
MainActivity.java
packagecom.example.mars_2500_ratingbar;
importandroid.support.v7.app.ActionBarActivity;
importandroid.support.v7.app.ActionBar;
importandroid.support.v4.app.Fragment;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.LayoutInflater;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.ViewGroup;
importandroid.widget.Button;
importandroid.widget.RatingBar;
importandroid.widget.RatingBar.OnRatingBarChangeListener;
importandroid.os.Build;
publicclassMainActivityextendsActivity
{
RatingBarratingBar;
Buttonbutton1;
publicstaticStringTAG="MainActivity";
@Override
protectedvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar=(RatingBar)findViewById(R.id.ratingBar1);
ratingBar.setOnRatingBarChangeListener(newRatingBarChangeListener());
button1=(Button)findViewById(R.id.buttn1);
button1.setOnClickListener(newClickListener());
}
classRatingBarChangeListenerimplementsOnRatingBarChangeListener
{
@Override
publicvoidonRatingChanged(RatingBarratingBar,floatrating,
booleanfromUser)
{
Log.i(TAG,"当前分数="+rating);
System.out.println("当前分数="+rating);
}
}
classClickListenerimplementsOnClickListener
{
@Override
publicvoidonClick(Viewv)
{//在当前加0.5分
ratingBar.setRating(ratingBar.getRating()+0.5f);
}
}
@Override
publicbooleanonCreateOptionsMenu(Menumenu)
{
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate(R.menu.main,menu);
returntrue;
}
}
activity_main.xml
<?xmlversion="1.0"encoding="UTF-8"?> <LinearLayoutxmlns: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之RatingBar控件用法详解的全部内容了,希望对大家有所帮助,多多支持毛票票~