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