Android Learning---Seekbar and ratingbar

Source: Internet
Author: User

SeekBar Drag bar: Drag bar and scroll bar similar, when the drag bar can drag the slider to change the progress

Ratingbar star rating bar: Star ratings bar similar to drag bars


Seekbar-specific XML attributes

ANDROID:THUMB specifies a Arawable object as the definition slider



Ratingbar-specific XML attributes

Android:isindicator whether the user is allowed to change (true to not allow modification)

How many stars are there in Android:numstars

Android:rating the default star rating

Android:stepsize change at least how many stars at a time



In order to know the progress of the change and do the corresponding operation, we need to add monitoring

SeekBar's Monitor Onseekbarchangelistener

Ratingbar's Monitor Onratingbarchangelistener


Let's get acquainted with them by example.

(1) Writing layout file Activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/linearlayout" Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > &        Lt;imageview android:id= "@+id/imgview1" android:layout_width= "120DP" android:layout_height= "100DP" android:src= "@drawable/ic_launcher"/> <seekbar android:id= "@+id/seekbar" Android:layout_widt H= "180DP" android:layout_height= "wrap_content" android:thumb= "@drawable/ic_launcher" android:max= "255 "android:progress=" 255 "/> <imageview android:id=" @+id/imgview2 "android:layout_width=" 12 0DP "android:layout_height=" 100DP "android:src=" @drawable/img01 "/> <ratingbar android:i D= "@+id/reatingbar" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an droid:max= "255" Android:p rogress= "255" android:rating= "5" android:stepsize= "0.5" android:numstars= "5"/> </lin Earlayout>

(2) Writing Mainactivity.java

Package Com.example.bar;import Android.annotation.targetapi;import Android.app.activity;import android.os.Build; Import Android.os.bundle;import android.util.log;import Android.view.menu;import Android.widget.imageview;import Android.widget.ratingbar;import Android.widget.ratingbar.onratingbarchangelistener;import Android.widget.SeekBar ; Import Android.widget.SeekBar.OnSeekBarChangeListener; @TargetApi (build.version_codes. Honeycomb) public class Mainactivity extends Activity {private ImageView IMGVIEWSB = null;//definition Imageviewprivate ImageView I  MGVIEWRB = null;private SeekBar SeekBar = null; Define seekbarprivate Ratingbar Ratingbar = null; Define ratingbar@overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//Layout Component THIS.IMGVIEWSB = (ImageView) Findviewbyid (R.ID.IMGVIEW1); THIS.IMGVIEWRB = (ImageView) Findviewbyid (r.id.imgview2); This.seekbar = (SeekBar) Findviewbyid (R.id.seekbar); This.ratingbar = (Ratingbar) FindviewbyId (R.id.reatingbar);//seekbar set Listener, change image transparency This.seekBar.setOnSeekBarChangeListener (new Onseekbarchangelistener () { @Overridepublic void Onstoptrackingtouch (SeekBar SeekBar) {log.i ("Onstoptrackingtouch", "Stop dragging triggered method"); @Overridepublic void Onstarttrackingtouch (SeekBar SeekBar) {log.i ("Onstarttrackingtouch", "Start dragging triggered method");} /** * Seekbar:seekbar Object * Progress: Drag Bar Progress * Fromuser: Whether the user manually change */@Overridepublic void onprogresschanged (SeekBar seekBar, I  NT Progress,boolean fromuser) {Imgviewsb.setalpha (progress); Sets the transparency of the picture log.i ("onprogresschanged", "the method triggered when the position of the drag slider Changes");}); /ratingbar set Monitor, change picture transparency This.ratingBar.setOnRatingBarChangeListener (new Onratingbarchangelistener () {/** * Ratingbar : Ratingbar Object * Rating: Star size * Fromuser: Whether the user manually change */@Overridepublic void onratingchanged (Ratingbar ratingbar, float rating , Boolean Fromuser) {imgviewrb.setalpha ((int) (RATING*255/5));}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuiNflater (). Inflate (R.menu.main, menu); return true;}} 

Once allowed, the effect is as follows:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.