The example of this article describes the development of Android programming Seekbar using handler message processing operations. Share to everyone for your reference, specific as follows:
This case simple implementation progress bar can go, can drag and drop function, below see source code:
Layout file:
<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 ">
<seekbar
android:id=" @+id/seekbar1 "android:layout_width=" Match_parent "
android:layout_height= "wrap_content"
android:layout_alignparentleft= "true"
android:layout_ Alignparenttop= "true"/>
</RelativeLayout>
Java files:
Package Com.example.lession16_seekbar;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.SeekBar;
public class Mainactivity extends activity {private SeekBar SeekBar;
private static final int max_progress = 100;
private int current_progress=10;
private static final int pro=1;
Private Handler Handler;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
SeekBar = (SeekBar) This.findviewbyid (R.ID.SEEKBAR1);
Seekbar.setmax (max_progress);
/* New Thread (new Runnable () {@Override public void run () {while (true) {try {thread.sleep (600);
current_progress+=10;
Seekbar.setprogress (current_progress);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); }}}). Start (); */handler = newHandler () {@Override public void handlemessage (message msg) {super.handlemessage (msg);
Switch (msg.what) {case Pro:if (current_progress>=max_progress) {}else{current_progress+=1;
Seekbar.incrementprogressby (1);
Handler.sendemptymessagedelayed (PRO, 600);
} break;
Default:break;
}
}
};
current_progress=current_progress>0?current_progress:0;
Seekbar.setprogress (current_progress);
Handler.sendemptymessage (PRO); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the action Bar if
It is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
Effect Diagram (Automatic walk):
I hope this article will help you with the Android program.