Android UI Component Advanced (1)--button with progress bar
Introduction to this section:
This series is the advanced series following the Android UI component instance, in which we will further learn
Android UI components, it is recommended to read this series of front-line learning UI component Instance Daquan series, master the use of basic components;
of course, you can also directly Learn this series! Well, don't say much, just start the first section! This sectionto demonstrates:
A button with a progress bar! I believe everyone in the 360 mobile phone assistant to see this stuff:
The SectionTo implementation of this is the bottom of the click to show the progress of the button
:
Essential Foundation:
1. Some properties of the progress bar:
background: set a background picture
Max : set the maximum value of the progress bar
Progress: set the value of the progress bar
style= "? Android:attr/progressbarstylehorizontal" : Defines the style of the progress bar as a horizontal direction
progressdrawable: when we do not want to use the system default progress can be defined by ourselves, this resource file is
used to call us The progress icon that you define is typically built under drawable. XML pieces using Layer-list to group
To weave these icons.
2.Handler Related methods:
The operation of the UI is either in the main thread or in handler, and the UI component should not be manipulated directly in the new thread, it will report the exception!
handlemessage (msg): use handle to override the main method, use Msg.what to determine the identification code, perform the corresponding operation
sendemptymessage (0x123) : Send an empty message to handle, the identifier is 0x123
sendemptymessagedelayed (0x321,500) send an empty message to handler, the identifier is 0x321, and the delay is 500 milliseconds .
Engineering Analysis:
Related code:
Mainactivity.java
Package Com.jay.uidemo.progressbuttondemo;import Android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.view.view;import Android.widget.button;import Android.widget.progressbar;import Android.widget.textview;public class Mainactivity extends Activity {int i=0; ProgressBar Progressbar=null; Button Downloadbtn=null; Handler handler=new Handler () {public void Handlemessage (Message msg) {switch (msg.what) {case 0x123: Downloadbtn.setclickable (false); i+=20;progressbar.setprogress (i); if (i!=100) {handler.sendemptymessagedelayed ( 0x123,500);d ownloadbtn.settext (i+ "%");} else if (i==100) {downloadbtn.settext ("Download Complete"); handler.sendemptymessagedelayed (0x321,500);} Break;case 0x321:downloadbtn.settext ("open");d ownloadbtn.setclickable (True);d Ownloadbtn.setbackgroundresource ( R.drawable.aa_button_after); handler.sendemptymessagedelayed (0x110,1000); Break;case 0x110: Progressbar.setprogress (0);d Ownloadbtn.setbackgroundresource (r.drawable.btn_selector);d ownloadbtn. SetText ("Download");d efault:break;};}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); TextView tx = (TextView) Findviewbyid (r.id.texttitle); Progressbar= (ProgressBar) Findviewbyid (R.id.progressbar); Downloadbtn= (Button) Findviewbyid (R.ID.DOWNLOADBTN); Downloadbtn.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {i= 0; Handler.sendemptymessage (0x123);}});}}
The Btn_selctor.xml in drawable
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_pressed=" false "android:drawable=" @android: Color/transparent "></ item> <item android:state_pressed= "true" android:drawable= "@drawable/aa_button_gray_pressed" ></ Item></selector>
The Progress_selctor.xml in drawable
<?xml version= "1.0" encoding= "Utf-8"? ><layer-list xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:id=" @android: id/progress "> <clip android:gravity=" left " android: cliporientation= "Horizontal" android:drawable= "@drawable/aa_button_normal"/> </item></ Layer-list>
Activity_main.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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.jay.uidemo.progressbuttondemo.MainActivity" > <textview android:id= "@+id/texttitle" an Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "button with input bar"/> <Rel Ativelayout android:layout_width= "fill_parent" android:layout_height= "50DP" android:gravity= "Bottom" Android:layout_centerhorizontal= "true" android:layout_centervertical= "true" > <progress Bar android:id= "@+id/progressbar" style="? Android:attr/progressbarstylehorizontal" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:background= "@drawable/aa_button_gray_normal" android:max= "Android" oid:progress= "0" android:progressdrawable= "@drawable/progress_selector"/> <button and Roid:id= "@+id/downloadbtn" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" android:text= "Download" android:layout_centerhorizontal= "true" Android:layout_centervertical= "Tru E "android:background=" @drawable/btn_selector "/> </relativelayout></relati Velayout>
Source Download:
Http://pan.baidu.com/s/1hql9qOc
Android UI Component Advanced (1)--button with progress bar