The Seekbar of Android control development

Source: Internet
Author: User

The Seekbar of Android control development This post focuses on the use of Seekbar, which is quite extensive when used in the player. Let's take a look at the code here!

Mainactivity.java:
Package Com.example.seekbar;


Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.SeekBar;


public class Mainactivity extends Activity {


Private SeekBar Myseekbar = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Myseekbar = (SeekBar) Findviewbyid (R.id.myseekbar);
Set the Seekbar progress maximum
Myseekbar.setmax (100);

Seekbar-bound listener
Myseekbar.setonseekbarchangelistener (New Setseekbarlistener ());
}

Creating a Seekbar Listener
Class Setseekbarlistener implements seekbar.onseekbarchangelistener{

This method is called when the progress bar is changed. Can be judged according to Fromuser, whether the user manually adjust the progress
Progress indicates the current position of the progress slider
@Override
public void onprogresschanged (SeekBar SeekBar, int progress,
Boolean Fromuser) {
TODO auto-generated Method Stub
if (Fromuser) {
SYSTEM.OUT.PRINTLN ("User change SeekBar to--" + progress);
}else{
SYSTEM.OUT.PRINTLN ("System change SeekBar to--" + progress);
}


}


Call this method when you manually start changing the position of the progress slider
@Override
public void Onstarttrackingtouch (SeekBar SeekBar) {
TODO auto-generated Method Stub
System.out.println ("Touch Start-to" + seekbar.getprogress ());
}


Call this method when you manually change the slider position to end
@Override
public void Onstoptrackingtouch (SeekBar SeekBar) {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Touch stop--" + seekbar.getprogress ());
}

}


@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}


}


Main layout file Mail.xml: <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">


<textview
Android:id= "@+id/mytext"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>

<seekbar
Android:id= "@+id/myseekbar"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/mytext"
/>


</RelativeLayout>


the effect of the implementation is as follows:

When the user drags the slider in Seekbar, the relevant method in the listener is called, and the following log is printed:


The Seekbar of Android control development

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.