Seekbar Control for Android Learning

Source: Internet
Author: User

Seekbar: A seekbar is an extension of progressbar that adds a draggable thumb. the user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. placing focusable widgets to the left or right of a seekbar is discouraged.

The above is an official definition.

What is the seekbar control? The seekbar control is actually a progress bar at an advanced level. It can be dragged like the progress bar on the player we are listening to songs and watching movies, a progress bar control that can change the progress! This is what it looks like:

The following describes how to use seekbar. For example, the function is very simple. The activity is a seekbar and a textview. When we drag the progress of seekbar, the corresponding progress changes are displayed in the textview below!

Step 1: Define Activity

Add a seekbar and a textview to the main. xml file.

<? 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"

>

<Seekbar

Android: Id = "@ + ID/seekbar"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

>

</Seekbar>

<Edittext

Android: Id = "@ + ID/edit"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

>

</Edittext>

</Linearlayout>

Step 2: Compile the activity

Package com. gufengxiachen. counter;

Import Android. App. activity;

Import Android. OS. Bundle;

Import Android. widget. seekbar;

Import Android. widget. seekbar. onseekbarchangelistener;

Import Android. widget. textview;

Public class counteractivity extends activity {

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

// Define a seekbar and a textview

 

Private seekbar;

Private textview;

@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

// Obtain the seekbar object based on the ID value

Seekbar = (seekbar) findviewbyid (R. Id. seekbar );

Seekbar. setmax (100 );

// Set the listener for seekbar (anonymous internal class is used here)

Seekbar. setonseekbarchangelistener (New onseekbarchangelistener (){

// Rewrite the three methods of onseebarchangelistener

// Onstarttrackingtouch at the first time, which is executed when the progress starts to change

@ Override

Public void onstarttrackingtouch (seekbar ){

// Todo auto-generated method stub

}

// The second method onprogresschanged is executed when the progress changes

@ Override

Public void onprogresschanged (seekbar, int progress,

Boolean fromuser ){

// Todo auto-generated method stub

Textview = (textview) findviewbyid (R. Id. Edit );

Int I = seekbar. getprogress ();

Textview. settext ("" + I );

}

// The third is onstoptrackingtouch, which is executed when the drag is stopped.

@ Override

Public void onstoptrackingtouch (seekbar ){

// Todo auto-generated method stub

Textview = (textview) findviewbyid (R. Id. Edit );

Int I = seekbar. getprogress ();

Textview. settext ("" + I );

}

});

}

}

After entering the simple steps above, a simple seekbar application is completed. In fact, many other controls use the same method. First, define the control, obtain the control object, and add the listener, finally, implement the function in the corresponding method of the listener !!

Finally, I wish you a pleasant learning experience !!!

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.