[Android development and learning 27] progress bar of interface controls (ProgressBar)

Source: Internet
Author: User

I. Basic Knowledge:

 

1. ProgressBar layout in the interface file XML:

[Html]
<ProgressBar android: id = "@ + id/progressbar_updown"
Android: layout_width = "200dp"
Android: layout_height = "wrap_content"
Style = "? Android: attr/progressBarStyleHorizontal"
Android: layout_gravity = "center_vertical"
Android: max = "100"
Android: progress = "50"
Android: secondaryProgress = "70">

<ProgressBar android: id = "@ + id/progressbar_updown"
Android: layout_width = "200dp"
Android: layout_height = "wrap_content"
Style = "? Android: attr/progressBarStyleHorizontal"
Android: layout_gravity = "center_vertical"
Android: max = "100"
Android: progress = "50"
Android: secondaryProgress = "70">

[Plain]
Style = "? Android: attr/progressBarStyleHorizontal "sets the style to long
Android: max = "100" maximum progress value is 100
Android: progress = "50" initialization progress Value
Android: secondaryProgress = "70" indicates the Second Progress value at the underlying layer during initialization.
Android: layout_gravity = "center_vertical" vertical center

Style = "? Android: attr/progressBarStyleHorizontal "sets the style to long
Android: max = "100" maximum progress value is 100
Android: progress = "50" initialization progress Value
Android: secondaryProgress = "70" indicates the Second Progress value at the underlying layer during initialization.
Android: layout_gravity = "center_vertical" vertical center

 


2. control and use of ProgressBar in the code file (. java:

[Java]
Private ProgressBar myProgressBar;
// Define ProgressBar
 
MyProgressBar = (ProgressBar) findViewById (R. id. progressbar_updown );
// ProgressBar obtains the ID from XML
 
MyProgressBar. incrementProgressBy (5 );
// Increase the progress value of ProgressBar by 5
 
MyProgressBar. incrementProgressBy (-5 );
// The Progress value of ProgressBar is reduced by 5
 
MyProgressBar. incrementSecondaryProgressBy (5 );
// The Progress value of the Second progress bar increases by 5
 
MyProgressBar. incrementSecondaryProgressBy (-5 );
// The Progress value of the Second progress bar is reduced by 5

Private ProgressBar myProgressBar;
// Define ProgressBar

MyProgressBar = (ProgressBar) findViewById (R. id. progressbar_updown );
// ProgressBar obtains the ID from XML

MyProgressBar. incrementProgressBy (5 );
// Increase the progress value of ProgressBar by 5

MyProgressBar. incrementProgressBy (-5 );
// The Progress value of ProgressBar is reduced by 5

MyProgressBar. incrementSecondaryProgressBy (5 );
// The Progress value of the Second progress bar increases by 5

MyProgressBar. incrementSecondaryProgressBy (-5 );
// The Progress value of the Second progress bar is reduced by 5

 

3. Important XML attributes

Android: progressBarStyle: Default progress bar style

Android: progressBarStyleHorizontal: horizontal style

 

 

4. Important Methods

[Plain]
GetMax (): Maximum range of the returned progress bar
 
GetProgress (): returns the progress
 
GetSecondaryProgress (): returns the secondary progress
 
IncrementProgressBy (int diff): Specifies the increase progress.
 
IsIndeterminate (): indicates whether the progress bar is in uncertain mode.
 
SetIndeterminate (boolean indeterminate): sets the uncertain mode.
 
SetVisibility (int v): sets whether the progress bar is visible.

GetMax (): Maximum range of the returned progress bar

GetProgress (): returns the progress

GetSecondaryProgress (): returns the secondary progress

IncrementProgressBy (int diff): Specifies the increase progress.

IsIndeterminate (): indicates whether the progress bar is in uncertain mode.

SetIndeterminate (boolean indeterminate): sets the uncertain mode.

SetVisibility (int v): sets whether the progress bar is visible.


 

 

 

 

Ii. Code display:

1. "Activity_09 \ src \ yan \ activity_09 \ MainActivity. java"

[Java]
Package yan. activity_09;
 
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ProgressBar;
Import android. app. Activity;
 
Public class MainActivity extends Activity {
// Declare Variables
Private ProgressBar firstBar = null;
Private ProgressBar secondBar = null;
Private Button myButton = null;
Private int progress_vol = 0;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

// Map the Control ID to the variable
FirstBar = (ProgressBar) findViewById (R. id. firstBar );
SecondBar = (ProgressBar) findViewById (R. id. secondBar );
MyButton = (Button) findViewById (R. id. myButton );


MyButton. setOnClickListener (new ButtonListenr ());
}

Class ButtonListenr implements OnClickListener {
 
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
If (0 = progress_vol)
{
// Set the maximum value of the progress bar
FirstBar. setMax (200 );
// Set the progress bar to a visible state
FirstBar. setVisibility (View. VISIBLE );
SecondBar. setVisibility (View. VISIBLE );
} Else if (progress_vol <firstBar. getMax ()){
// Set the current value of the main progress bar
FirstBar. setProgress (progress_vol );
// Set the current value of the Second progress bar
FirstBar. setSecondaryProgress (progress_vol + 10 );
// The progress bar cannot be displayed by default.
// SecondBar. setProgress (progress_vol );
} Else {
// Set the progress bar to an invisible state
FirstBar. setVisibility (View. GONE );
SecondBar. setVisibility (View. GONE );
}
Progress_vol + = 10;
}

}
}

Package yan. activity_09;

Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ProgressBar;
Import android. app. Activity;

Public class MainActivity extends Activity {
// Declare Variables
Private ProgressBar firstBar = null;
Private ProgressBar secondBar = null;
Private Button myButton = null;
Private int progress_vol = 0;
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

// Map the Control ID to the variable
FirstBar = (ProgressBar) findViewById (R. id. firstBar );
SecondBar = (ProgressBar) findViewById (R. id. secondBar );
MyButton = (Button) findViewById (R. id. myButton );


MyButton. setOnClickListener (new ButtonListenr ());
}
 
Class ButtonListenr implements OnClickListener {

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
If (0 = progress_vol)
{
// Set the maximum value of the progress bar
FirstBar. setMax (200 );
// Set the progress bar to a visible state
FirstBar. setVisibility (View. VISIBLE );
SecondBar. setVisibility (View. VISIBLE );
} Else if (progress_vol <firstBar. getMax ()){
// Set the current value of the main progress bar
FirstBar. setProgress (progress_vol );
// Set the current value of the Second progress bar
FirstBar. setSecondaryProgress (progress_vol + 10 );
// The progress bar cannot be displayed by default.
// SecondBar. setProgress (progress_vol );
} Else {
// Set the progress bar to an invisible state
FirstBar. setVisibility (View. GONE );
SecondBar. setVisibility (View. GONE );
}
Progress_vol + = 10;
}

}
}

 

2. "Activity_09 \ res \ layout \ main. xml"

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "#00 aaaa"
>
<TextView
Android: id = "@ + id/firstText"
Android: text = "@ string/hello_world"
Android: gravity = "center_vertical"
Android: textSize = "15pt"
Android: background = "# aa0000"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: singleLine = "true"/>
<ProgressBar
Android: id = "@ + id/firstBar"
Style = "? Android: attr/progressBarStyleHorizontal"
Android: layout_width = "200dp"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
/>
<ProgressBar
Android: id = "@ + id/secondBar"
Style = "? Android: attr/progressBarStyle"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
/>
<Button
Android: id = "@ + id/myButton"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "begin"
/>
</LinearLayout>

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "#00 aaaa"
>
<TextView
Android: id = "@ + id/firstText"
Android: text = "@ string/hello_world"
Android: gravity = "center_vertical"
Android: textSize = "15pt"
Android: background = "# aa0000"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: singleLine = "true"/>
<ProgressBar
Android: id = "@ + id/firstBar"
Style = "? Android: attr/progressBarStyleHorizontal"
Android: layout_width = "200dp"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
/>
<ProgressBar
Android: id = "@ + id/secondBar"
Style = "? Android: attr/progressBarStyle"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
/>
<Button
Android: id = "@ + id/myButton"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "begin"
/>
</LinearLayout>
3. "Activity_09 \ res \ values \ strings. xml"

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
 
<String name = "app_name"> Activity_09 </string>
<String name = "hello_world"> Hello world! </String>
<String name = "menu_settings"> Settings </string>
 
</Resources>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>

<String name = "app_name"> Activity_09 </string>
<String name = "hello_world"> Hello world! </String>
<String name = "menu_settings"> Settings </string>

</Resources>

 

 

 

 

Iii. effect display:

 

 

 

 

 

 

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.