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 }
时间: 2024-11-03 20:02:07

ANDROID_MARS学习笔记_S01_012_RatingBar的相关文章

ANDROID_MARS学习笔记_S03_007_GoogleMap1

一.简介 二.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fi

ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求

一.代码 1.xml(1)activity_main.xml 1 <LinearLayout 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="

ANDROID_MARS学习笔记_S02_012_ANIMATION_利用AnimationListener在动画结束时删除或添加组件

一.代码 1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/layoutId" 4 android:orientation="

ANDROID_MARS学习笔记_S01原始版_013_广播机制二

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_012_广播机制一

一.简介 二.代码1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width

ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

一.运行结果 一.代码1.xml(1)activity_main.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_heig

ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S02_004_ExpandableListActivity

1.main.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"

ANDROID_MARS学习笔记_S02_005_AppWidget1

一.AppWidget介绍 1.Widget的定义创建流程 (1)在res新建xml文件夹,新建appwidget-provider.xml,为widget提供元数据 (2)在res/layout文件夹下定义widget的样式 (3)在src新建类,extends AppWidgetProvider,自定义widget (4)在AndroidManifest.xml中把自定义的widget添加为receiver,接收APPWIDGET_UPDATE广播 2. 二.代码 1.res/xml/exa