A simple example of Android in ProgressDialog

Internet generally are an example of how the progress bar shows how close the end of the mandate is not an article, reference to other articles through the tests to set the progress bar shows a simple example is as follows:

The establishment of android and engineering work is omitted, Google click on it.

Here to introduce the main Activity
ProgressBarDemo.java

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();
    }

    /**
     *  Used to update UI Handler
     */
    private Handler handler = new Handler(){

                @Override
                public void handleMessage(Message msg) {

                        // Close ProgressDialog
                        progressDialog.dismiss();

                        // Update the UI
                        statusTextView.setText("Completed!");
                }};

        /**
         *  Click on the button event listener
         */
    private void setListener(){
        beginBtn.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                                // Show ProgressDialog
                                progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);

                                // Create a new thread
                                new Thread(){

                                        @Override
                                        public void run() {
                                                // Need to spend time calculation method
                                                Calculation.calculate(4);

                                                // To send a message handler
                                                handler.sendEmptyMessage(0);
                                        }}.start();
                        }
                });
    }

}

Calculation.java

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 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 the android, usually we can not in a separate thread to update UI, but in the main thread, which is why we want to use Handler, and when the handler receive the message, it will put it into the queue waiting for the implementation of , generally speaking this will soon be executed.

A simple example of Android in ProgressDialog

分类:Mobile 时间:2010-03-29 人气:2213
分享到:
blog comments powered by Disqus

相关文章

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp

  • Why public static void main (String args []){} 2010-11-06

    main () is a Java program entry, program execution is the beginning of this entry. Class is a class member, main () method must be public members. So that it can be called in the execution environment. main () method does not produce the object can b

  • public static final naming convention 2010-07-12

    Has been, public static final variable is the standard name is all uppercase, words underlined between. The problem is, we really feel IS_HAS_CERTIFIED better than IsHasCertified you read a good maintenance. Public static final project capital hump w

  • [Transfer] C / C + + language to explore the deep void and void pointer 2010-10-22

    1. Overview many beginners on the C / C + + language in the void and the void pointer type do not understand, so in the use of some errors. This paper will be void explain the profound meaning of keywords and void and the void pointer type detailing

  • 深入理解void以及void指针的含义 2014-12-16

    本篇文章对小编void以及void指针的含义进行了详细的分析和介绍.需要的朋友参考下 void的含义void即"无类型",void *则为"无类型指针",可以指向任何数据类型. void指针使用规范①void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值.例如: int *pint; void *pvoid; pvoid = pint; /* 不过不能 pint = pvoid; */ 如果要将pvoid赋给其他类型指针,则需要强制类型转换

  • Java Package Summary 2010-12-19

    Background: Recently downloaded a small open source projects, individuals made some changes and want to be labeled jar package as a custom expansion pack to use. --------- --------------- Packaged with Eclipse 1, the project does not reference an ext

  • Collection of java applications 4: Save large object Upload progress bar shows 2010-07-20

    Using fileupload upload documents, and save blob field, show progress in the process of uploading. <form name="uploadform" method="POST" action="upload.action" ENCTYPE="multipart/form-data" target="hidden&qu

  • Java package for reading resources or JAR file path 2010-07-26

    Here are divided into two specific: The first: the resource file for the general suffix file The second: Resource file for picture files First] [NO1 Use this line of code can get class path of the root class String path = Thread.currentThread (). Get

  • Query code on the java package flip (jsp + struts + spring) 2010-09-25

    1.page object: used to flip the package information public class Pages ( / ** * Current Page * / private int currentPage = 1; @ SuppressWarnings ("unused") / ** * Total number of pages * / private int maxPage = 0; / ** * Number of records per pa

  • ECLIPSE JSP project on the issue under the java package 2010-11-25

    package grad.util; /** * jacob Instance manipulation class */ import java.io.File; import java.io.*; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class dealWord { public static boolean

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.304 (s). 14 q(s)