ANDROID_MARS学习笔记_S01_012_RatingBar

1.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <RatingBar
12         android:id="@+id/firstRatingBar"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:numStars="4"
16         android:stepSize="1"
17          />
18     
19     <Button 
20         android:id="@+id/button"
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23         android:layout_below="@id/firstRatingBar"
24         android:text="button"/>
25 
26 </RelativeLayout>

 

2.java

 1 package com.marschen.s01e_e18_ratingbar;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Menu;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 import android.widget.RatingBar;
10 import android.widget.RatingBar.OnRatingBarChangeListener;
11 
12 public class MainActivity extends Activity {
13 
14     private RatingBar ratingBar;
15     private Button button;
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         
21         ratingBar = (RatingBar)findViewById(R.id.firstRatingBar);
22         button = (Button)findViewById(R.id.button);
23         
24         RatingBarListener listener = new RatingBarListener();
25         ratingBar.setOnRatingBarChangeListener(listener);
26         
27         ButtonListener buttonListener = new ButtonListener();
28         button.setOnClickListener(buttonListener);
29     }
30 
31     class ButtonListener implements OnClickListener{
32 
33         @Override
34         public void onClick(View v) {
35             ratingBar.setRating(ratingBar.getRating() + 1.0f);
36         }
37         
38     }
39     
40     class RatingBarListener implements OnRatingBarChangeListener{
41 
42         @Override
43         public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
44             System.out.println("rating:" + rating + ",fromUser:" + fromUser);
45         }
46         
47     }
48     @Override
49     public boolean onCreateOptionsMenu(Menu menu) {
50         // Inflate the menu; this adds items to the action bar if it is present.
51         getMenuInflater().inflate(R.menu.main, menu);
52         return true;
53     }
54 
55 }

 

你可能感兴趣的:(ANDROID_MARS学习笔记_S01_012_RatingBar)