Progress bar:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <ProgressBarDefault progress bar Android:id= "@+id/pb1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <ProgressBarAndroid:id= "@+id/pb2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"style= "@android: Style/widget.progressbar.horizontal"Android:max= "+"maximum value is android:progress= "+"/>the initial value is<ButtonAndroid:id= "@+id/btndownload"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Download File"/></LinearLayout>
Packagecom.sxt.day03_07;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.SystemClock;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.ProgressBar; Public classMainactivityextendsActivity {ProgressBar mprogressbar; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); Setlistener (); } Private voidSetlistener () {Findviewbyid (r.id.btndownload). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {NewThread () {//Open A thread, then the progress bar work, does not affect the main thread of the work, if not multi-threading the progress bar to work when you click somewhere else there will be problems Public voidrun () { for(inti=1;i<=100;i++) {mprogressbar.setprogress (i); Systemclock.sleep (20);//Systemclock.sleep (20) is an Android pause, and the progress bar changes to keep the current thread dormant for 10 milliseconds } }; }.start (); } }); } Private voidInitview () {Mprogressbar=(ProgressBar) Findviewbyid (R.ID.PB2); Mprogressbar.setprogress (0); }}
Drag the bar:
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <SeekBarAndroid:id= "@+id/seekbar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:max= "+"android:progress= " the"/></Relativelayout>
Packagecom.sxt.day03_08;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.widget.SeekBar;ImportAndroid.widget.SeekBar.OnSeekBarChangeListener; Public classMainactivityextendsActivityImplementsonseekbarchangelistener{intmprogress;//represents the current progress value of Seekbar@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); SeekBar SeekBar=(SeekBar) Findviewbyid (R.id.seekbar); Seekbar.setonseekbarchangelistener ( This);//Add a drag listener to Seekbar} @Override Public voidOnprogresschanged (SeekBar SeekBar,intProgress,BooleanFromuser) {//This function is always called in drag, Fromuser is true for touch operation, progress represents the current drag value,mprogress=progress; LOG.I ("Main", "progress=" +progress); } @Override Public voidOnstarttrackingtouch (SeekBar SeekBar) {//Start Drag functionLOG.I ("main", "Start dragging Seekbar"); } @Override Public voidOnstoptrackingtouch (SeekBar SeekBar) {//End Drag functionLOG.I ("main", "End drag Seekbar"); }}
Android 14 progress bar and drag bar