Basic android control learning ----- SeekBar & amp; RatingBar, basic android Control

Source: Internet
Author: User

Basic android control learning ----- SeekBar & RatingBar, basic android Control

SeekBar and RatingBar

1. SeekBar (drag bar)

(1) A simple understanding of a drag is a line that can be dragged. This is very common for us, such as video playback or music playback. Below we will summarize some common attributes, many Attributes are the same as those of ProgressBar.
.

Android: max: set the maximum value of the slider bar

Android: progress: indicates the value of the current slider.

Android: secondaryProgress: Level 2 Slider

Android: thumb: Set the drawable of the slider.

These attributes can also be called and set in Java.

(2) SeeBar listening events should not be unfamiliar with listening events. There is an important knowledge point listening mechanism in j2SE. If you have learned JavaScript, this concept is also called events.

OnProgressChanged: triggered when the progress bar changes

OnStartTrackingTouch: triggered when SeekBar is installed

OnStopTrackingTouch: triggered when SeekBar is opened

(3) Simple instances

<? 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"> <SeekBar android: id = "@ + id/sb1" android: layout_width = "match_parent" android: layout_height = "50dp" android: max = "100" android: thumb = "@ mipmap/ic_launcher"/> <TextView android: id = "@ + id/tv1" android: layout_width = "match_parent" android: layout_height = "50dp" android: layout_marginTop = "10dp" android: gravity = "center_vertical" android: text = "Seek's current value is 0" android: textSize = "24sp"/> </LinearLayout>

Java code

Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. widget. seekBar; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {private TextView textView; private SeekBar seekBar; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); seekBar = (SeekBar) findViewById (R. id. sb1); textView = (TextView) findViewById (R. id. tv1); seekBar. setOnSeekBarChangeListener (new SeekBar. onSeekBarChangeListener () {@ Override public void onProgressChanged (SeekBar seekBar, int I, boolean B) {textView. setText ("the current value of SeekBar is" + I);} @ Override public void onStartTrackingTouch (SeekBar seekBar) {Toast. makeText (MainActivity. this, "you hold down SeekBar", Toast. LENGTH_LONG ). show () ;}@ Override public void onStopTrackingTouch (SeekBar seekBar) {Toast. makeText (MainActivity. this, "you have released SeekBar", Toast. LENGTH_LONG ). show ();}});}}

(4) custom SeekBar

Step 1: Define a stateListDrawable file in drawable and name it sb_thumb.xml.

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"  android:drawable="@mipmap/press"/>    <item android:state_pressed="false" android:drawable="@mipmap/nomal"/></selector>

Step 2: Define a layer-list in drawable. The stacked image contains the background and current progress bar, and is named sb_bar.xml.

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@android:id/background">        <shape>            <solid android:color="#FFFFD042" />        </shape>    </item>    <item android:id="@android:id/progress">        <clip>            <shape>                <solid android:color="#FF96E85D" />            </shape>        </clip>    </item></layer-list>

Step 3: Use

<?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">    <SeekBar        android:id="@+id/sb1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="100"        android:progressDrawable="@drawable/sb_bar"        android:thumb="@drawable/sb_thumb"/></LinearLayout>

:

Ii. RatingBar (star rating bar)

(1) From this term, we can easily imagine the star rating bar. When shopping online, there are often stores that let us evaluate the rating for five stars, the following summarizes common attributes:

Android: isIndicator: Indicates whether to use it. You cannot change it. The default value is false.

Android: numStars: number of stars displayed, which must be an integer

Android: rating: the default rating value must be a floating point number.

Android: stepSize: the value added for each rating. It must be a floating point style. Which of the following options is available in the style system? Android: attr/ratingBarStyleSmall and? Android: attr/ratingBarStyleIndicator

(2) event processing: you only need to set OnRatingBarChangeListener for RatinBar.

(3) Simple instances

<?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">   <RatingBar       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:rating="3.5"/></LinearLayout>

(4) custom RatingBar

Step 1: Write a layer-list file like the previous SeekBar: ratingbar_full.xml:

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@android:id/background" android:drawable="@mipmap/press"/>    <item android:id="@android:id/progress" android:drawable="@mipmap/nomal"/></layer-list>

Step 2: Use

Step 3: Use the defined style in the layout File

Related Article

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.