Examples of how the progress bar is displayed on the web, there is no article on how to close the task at the end, refer to the other article after the experiment to show the complete set of progress bar Simple example is as follows:
Set up Android projects and other jobs are omitted, Google a bit.
The main activity is described below.
Progressbardemo.java
Java code
- Package Com.lveyo.android.demo.progressbar;
- Import android.app.Activity;
- Import Android.app.ProgressDialog;
- Import Android.os.Bundle;
- Import Android.os.Handler;
- Import Android.os.Message;
- Import Android.view.View;
- Import Android.widget.Button;
- Import Android.widget.TextView;
- Public class Progressbardemo extends Activity {
- private TextView Statustextview;
- private Button beginbtn;
- private ProgressDialog ProgressDialog;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Statustextview = (TextView) Findviewbyid (r.id.status);
- BEGINBTN = (Button) Findviewbyid (R.ID.BEGINBTN);
- Setlistener ();
- }
- /**
- * Use handler to update the UI
- */
- private Handler Handler = new Handler () {
- @Override
- public void Handlemessage (Message msg) {
- //Close ProgressDialog
- Progressdialog.dismiss ();
- //Update UI
- Statustextview.settext ("completed!");
- }};
- /**
- * Click Button Event Listener
- */
- private void Setlistener () {
- Beginbtn.setonclicklistener (new View.onclicklistener () {
- @Override
- public void OnClick (View v) {
- //Display ProgressDialog
- ProgressDialog = Progressdialog.show (Progressbardemo. This, "Loading ...", "Please wait ...", true, false);
- //New Thread
- New Thread () {
- @Override
- public Void Run () {
- //methods that take time to calculate
- Calculation.calculate (4);
- //Send message to Handler
- Handler.sendemptymessage (0);
- }}.start ();
- }
- });
- }
- }
Calculation.java
Java code
- Package Com.lveyo.android.demo.progressbar;
- /**
- * Schematic method
- * @author Lveyo
- *
- */
- Public class Calculation {
- public static void calculate (int sleepseconds) {
- try {
- Thread.Sleep (Sleepseconds * 1000);
- } catch (Exception e) {
- //Todo:handle exception
- }
- }
- }
Main.xml file
XML code
- <? 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"
- >
- <TextView android:id="@+id/status"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <button android:id="@+id/beginbtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Begin"
- />
- </linearlayout>
In Android, we usually can't update the UI in a separate thread, but in the main thread, which is why we're using handler, and when handler receives the message, it puts it in the queue and waits for execution, which is usually done quickly.
- Progressbardemo.zip (5.2 KB)
- Download number of times: 1066
A simple example of ProgressDialog in Android