Android UI component (1)-buttons with progress bars
Android UI component (1)-buttons with progress bars
This section introduces:
This series is an advanced series following the Android UI component instance list, in which we will further learn
For Android UI components, we recommend that you read the complete series of UI component instances in this series of front-line learning courses to learn how to use basic components;
Of course, you can also directly learn this series! Okay. Let's get started with the first section! This section demonstrates the following:
Button with progress bar! I believe you can see this on the 360 mobile ASSISTANT:
In this section, you must click the button below to display the progress.
:
Prerequisites:
1. Some Properties of the progress bar:
Background: Set the background image
Max: sets the maximum value of the progress bar.
Progress: Set the progress bar value
Style =? Android: attr/progressBarStyleHorizontal: defines the style of the progress bar in the horizontal direction.
ProgressDrawable: if you do not want to use the system's default Progress, you can define one by yourself. This resource file is
To call our own Progress icon, we usually create a. xml file under drawable and use layer-list to group it.
Weave these icons.
2. Handler related methods:
UI operations are either performed in the main thread or handler. Do not operate the UI components directly in the new thread. An exception is reported!
HandleMessage (msg): The main method that Handle needs to override. use msg. what to judge the ID code and perform corresponding operations.
SendEmptyMessage (0x123): sends an empty message to handle. The ID code is 0x123.
SendEmptyMessageDelayed (0x321,500); sends an empty message to handler with an ID code of 0x321. The message is sent after a delay of 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); downLoadBtn. setText (I + %);} else if (I = 100) {downLoadBtn. setText (Download complete); handler. sendEmptyMessageDelayed (0x321,500);} break; case 0x321: downLoadBtn. setText (open); downLoadBtn. setClickable (true); downLoadBtn. setBackgroundResource (R. drawable. aa_button_after); handler. sendEmptyMessageDelayed (0x110); break; case 0 x: progressBar. setProgress (0); downLoadBtn. setBackgroundResource (R. drawable. btn_selector); downLoadBtn. setText (download); default: 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 );}});}}
Btn_selctor.xml in drawable
Progress_selctor.xml in drawable
Activity_main.xml: