阅读《Android 从入门到精通》(19)——评分条

评分条(RatingBar)

java.lang.Object;
android.view.View;
android.widget.ProgressBar;
android.widget.AbsSeekBar;
android.widget.RatingBar;

RatingBar 类方法

阅读《Android 从入门到精通》(19)——评分条_第1张图片

阅读《Android 从入门到精通》(19)——评分条_第2张图片

阅读《Android 从入门到精通》(19)——评分条_第3张图片

RatingBar 示例

完整工程:http://download.csdn.net/detail/sweetloveft/9424271

下述程序,学习的是 Android 最后一个官方原有控件了,此后我们要学习布局,等整个 Android 应用学完之后,我们就要开始思考:为什么是是这样?如何设计自己的控件?自绘控件、派生控件、组合控件,以及前三者融合起来的控件?如果在学习控件的组成上自己实现 OO 思想?我们需要在整个 Android 应用学完后结合 DuiLib 去反思这些设计,制作自己的设计,一点点的深入底层!

阅读《Android 从入门到精通》(19)——评分条_第4张图片

1.MainActivity.java

package com.sweetlover.activity;

import com.sweetlover.ratingbar.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView[] textView = new TextView[3];
	private RatingBar[] ratingBar = new RatingBar[3];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		textView[0] = (TextView) findViewById(R.id.textView1);
		textView[1] = (TextView) findViewById(R.id.textView2);
		textView[2] = (TextView) findViewById(R.id.textView3);
		ratingBar[0] = (RatingBar) findViewById(R.id.ratingBar1);
		ratingBar[1] = (RatingBar) findViewById(R.id.ratingBar2);
		ratingBar[2] = (RatingBar) findViewById(R.id.ratingBar3);

		ratingBar[0].setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				// TODO Auto-generated method stub
				int numStars = ratingBar.getNumStars();
				float curNum = rating;
				textView[0].setText("总共 " + numStars + " 个星:" + curNum);
			}
		});

		ratingBar[1].setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				// TODO Auto-generated method stub
				int numStars = ratingBar.getNumStars();
				float curNum = rating;
				textView[1].setText("<span style="font-family: Arial, Helvetica, sans-serif;">总共</span> " + numStars + " 个星:" + curNum);
			}
		});

		ratingBar[2].setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				// TODO Auto-generated method stub
				int numStars = ratingBar.getNumStars();
				float curNum = rating;
				textView[2].setText("总共 " + numStars + " 个星:" + curNum);
			}
		});
	}
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="30dp" >

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RatingBar
        android:id="@+id/ratingBar2"
        style="android:ratingBarStyleIndicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RatingBar
        android:id="@+id/ratingBar3"
        style="android:ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

3.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sweetlover.ratingbardemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.sweetlover.activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

你可能感兴趣的:(阅读《Android 从入门到精通》(19)——评分条)