Monday, May 21, 2012 : Article > Android User : Register | Login
บทความ
Webboard
วีดีโอ
บทความจากเพื่อนๆ Guru
17
มาเช็ค Ratting กันดีกว่า..... RatingBar เป็นส่วนขยายของ SeekBar และ ProgressBar ที่แสดงถึงการให้คะแนนที่อยู่ในรูปดาว ผู้ใช้สามารถสัมผัส คลิก หรือลากที่รูปดาวเพื่อตั้งค่าการให้คะแนน

1.สร้างโปรเจค โดยใช้ชื่อว่า AndroidRatingBar


2.ใน file main.xml  ( res>layout ) ใส่โค้ดดังต่อไป

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:orientation="vertical"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   >

<TextView

   android:layout_width="fill_parent"

   android:layout_height="wrap_content"

   android:text="@string/hello"

   />

<RatingBar

   android:layout_width="wrap_content"

   android:layout_height="wrap_content"

   style="?android:attr/ratingBarStyleIndicator"

   android:id="@+id/ratingbar_Indicator"

   />

<RatingBar

   android:layout_width="wrap_content"

   android:layout_height="wrap_content"

   style="?android:attr/ratingBarStyleSmall"

   android:id="@+id/ratingbar_Small"

   android:numStars="20"

   />

<RatingBar

   android:layout_width="wrap_content"

   android:layout_height="wrap_content"

   style="?android:attr/ratingBarStyle"

   android:id="@+id/ratingbar_default"

   />

</LinearLayout>

 


3. เปิด file AndroidRatingBar.java ( src>th.co.bighead.exsample.AndroidRatingBar) และใส่โค้ดต่อไปนี้

package th.co.bighead.exsample.AndroidRatingBar;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.RatingBar;

import android.widget.Toast;

 

public class AndroidRatingBar extends Activity {

   /** Called when the activity is first created. */

   @Override

   public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

     

       final RatingBar ratingBar_Small = (RatingBar)findViewById(R.id.ratingbar_Small);

       final RatingBar ratingBar_Indicator = (RatingBar)findViewById(R.id.ratingbar_Indicator);

       final RatingBar ratingBar_default = (RatingBar)findViewById(R.id.ratingbar_default);

     

       ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){

 

   @Override

   public void onRatingChanged(RatingBar ratingBar, float rating,

     boolean fromUser) {

    // TODO Auto-generated method stub

    ratingBar_Small.setRating(rating);

    ratingBar_Indicator.setRating(rating);

    Toast.makeText(AndroidRatingBar.this, "rating:"+String.valueOf(rating),

      Toast.LENGTH_LONG).show();

   }});

   }

}


4.รันโปรแกรม





ลิงค์ที่เกี่ยวข้อง : RatingBar

Post Rating

Comments

There are currently no comments, be the first to post one.

Post Comment

Only registered users may post comments.
Home | Article | Webboard | Video | Blog | Showcase | News
Copyright 2010 by devguru.mobi