from: http://byandby.iteye.com/blog/817214
progress bar dialog to define clearly necessary to use ProgressDialog, first of all we need to create ProgressDialog object, of course, here also uses threads to control the progress of the show, in addition you can use the following methods to set ProgressDialog.
setProgressStyle: Setting progress bar style, the style is round, rotating.
setTitlt: Set ProgressDialog title
setMessage: Set ProgressDialog message;
setIcon: Set ProgressDialog title icon;
setIndeterminate: Set ProgressDialog progress bar is not clear;
setCancelable: Set ProgressDialog can press the return key to cancel;
setButton: ProgressDialog set a Button (need to listen Button event);
show: Show ProgressDialog.
First, let us look at the effect it run
Here take a look at the layout file
< span> android: orientation = "vertical"
android: layout_width = "fill_parent" < br /> android: layout_height = "fill_parent"
>
android: layout_width = "fill_parent"
android: layout_height = "wrap_content"
android: text = "@ string / hello"
/>
android: id = "@ + id/Button01 "
android: layout_width =" wrap_content "
android: layout_height =" wrap_content "
android: text = "circular progress bar" />
< br /> android: id = "@ + id/Button02"
android: layout_width = "wrap_content"
< span> android: layout_height = "wrap_content"
android: text = "elongated progress bar" />
Activity01
- package xiaohang.zhimeng;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class Activity01 extends Activity {
- private Button xhButton01, xhButton02;
- int xh_count = 0 ;
- / / declare the progress bar dialog
- ProgressDialog xh_pDialog;
- @ Override
- public void onCreate (Bundle savedInstanceState) {
- super . onCreate (savedInstanceState); < / li>
- setContentView (R.layout.main);
- / / get the button object < / li>
- xhButton01 = (Button) findViewById (R.id.Button01); < / li>
- xhButton02 = (Button) findViewById (R.id.Button02); < / li>
- / / Set xhButton01 event listener
- xhButton01.setOnClickListener ( new OnClickListener () { ;
- @ Override
- public void onClick (View v) {
- / / Create ProgressDialog object
- xh_pDialog = new ProgressDialog (Activity01. this );
- / / Set the progress bar style, it is round, rotating
- xh_pDialog.setProgressStyle (ProgressDialog.STYLE_SPINNER); ;
- / / Set ProgressDialog title
- xh_pDialog.setTitle ( "Prompt" );
- / / Set ProgressDialog message
- xh_pDialog.setMessage ( "This is a circular progress bar dialog" );
- / / Set ProgressDialog Title Icon
- xh_pDialog.setIcon (R.drawable.img1); ;
- / / Set ProgressDialog is not clear whether the progress bar is not set to false is not clear
- xh_pDialog.setIndeterminate ( false );
- / / Set ProgressDialog can press the return key to cancel
- xh_pDialog.setCancelable ( true );
- / / Set a ProgressDialog Button
- xh_pDialog.setButton ( "OK" , new Bt1DialogListener ());
- / / Let ProgressDialog show
- xh_pDialog.show ();
- }
- });
- / / Set xhButton02 event listener
- xhButton02.setOnClickListener ( new Button.OnClickListener () { ;
- @ Override
- public void onClick (View v) {
- xh_count = 0 ;
- / / Create ProgressDialog object
- xh_pDialog = new ProgressDialog (Activity01. this );
- / / Set the progress bar style, it is round, rotating
- xh_pDialog.setProgressStyle (ProgressDialog.STYLE_HORIZONTAL); ;
- / / Set ProgressDialog title
- xh_pDialog.setTitle ( "Prompt" );
- / / Set ProgressDialog message
- xh_pDialog.setMessage ( "This is a long-shaped progress bar dialog" );
- / / Set ProgressDialog Title Icon
- xh_pDialog.setIcon (R.drawable.img2); ;
- / / Set ProgressDialog is not clear whether the progress bar is not set to false is not clear
- xh_pDialog.setIndeterminate ( false );
- / / Set ProgressDialog Progress bar
- xh_pDialog.setProgress ( 100 );
- / / Set ProgressDialog can press the return key to cancel
- xh_pDialog.setCancelable ( true );
- / / Let ProgressDialog show
- xh_pDialog.show ();
- new Thread () {
- @ Override
- public void run () {
- try {
- while (xh_count <= 100 ) ; {
- / / by the thread to control the progress < / li>
- xh_pDialog.setProgress (xh_count + +);
- Thread.sleep ( 100 );
- }
- xh_pDialog.cancel ();
- } catch (Exception e) {
- xh_pDialog.cancel ();
- }
- } ;
- }. start ();
- }
- });
- }
- / / xhButton01 listener class
- class Bt1DialogListener implements DialogInterface.OnClickListener {
- @ Override
- public void onClick (DialogInterface dialog, int which) {
- / / Click the "OK" button to cancel the dialog
- dialog.cancel ();
- }
- }
- }
没有评论:
发表评论