本文主要是介绍蜗牛—Android基础之RatingBar,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java代码:
package com.wjj.ratingbar;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;public class MainActivity extends Activity {private RatingBar bar;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bar = (RatingBar) findViewById(R.id.firstRatingBar);RatingBarListener ratingBarListener = new RatingBarListener();bar.setOnRatingBarChangeListener(ratingBarListener);button = (Button) findViewById(R.id.firstButton);ButtonLitener buttonLitener = new ButtonLitener();button.setOnClickListener(buttonLitener);}class ButtonLitener implements OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubbar.setRating(bar.getRating() + 1.0f); // 点击一次RatingBar加1}}class RatingBarListener implements OnRatingBarChangeListener {@Overridepublic void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {// TODO Auto-generated method stubSystem.out.println("rating:" + rating); // 显示rating}}@Overridepublic 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;}}
XML代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><!-- numStars:星的个数 stepSize:步长 --><RatingBarandroid:id="@+id/firstRatingBar"android:layout_width="wrap_content"android:layout_height="wrap_content"android:numStars="5"android:stepSize="1" /><Buttonandroid:id="@+id/firstButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/firstRatingBar" /></RelativeLayout>
这篇关于蜗牛—Android基础之RatingBar的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!