Android组件 稍等提示(ProgressDialog)

/res/layout/main.xml代码如下:

[html] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <Button  
  7.         android:id="@+id/btn_circle"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="圆形进度条" />  
  11.     <Button  
  12.         android:id="@+id/btn_long"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="长形进度条" />  
  16. </LinearLayout>  

Java代码如下:

[java] view plain copy print ?
  1. package com.demo.android.progressDialog;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ProgressDialog;  
  5. import android.content.DialogInterface;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     private Button btnCircle;  
  12.     private Button btnLong;  
  13.     private ProgressDialog progressDialog;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         findViews();  
  19.     }  
  20.     private void findViews() {  
  21.         btnCircle=(Button) findViewById(R.id.btn_circle);  
  22.         btnCircle.setOnClickListener(btnCircleOnClickListener);  
  23.         btnLong=(Button) findViewById(R.id.btn_long);  
  24.         btnLong.setOnClickListener(btnLongOnClickListener);  
  25.     }  
  26.     /** 
  27.      * 显示圆形进度条 
  28.      */  
  29.     private View.OnClickListener btnCircleOnClickListener=new View.OnClickListener() {  
  30.         @Override  
  31.         public void onClick(View v) {  
  32.             progressDialog=new ProgressDialog(MainActivity.this);  
  33.             //设置进度条的样式为圆形   
  34.             progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  35.             progressDialog.setTitle("提示");  
  36.             progressDialog.setMessage("数据加载中,请稍后....");  
  37.             //设置进度条的图标   
  38.             progressDialog.setIcon(R.drawable.android);  
  39.             //设置进度条是否为不明确   
  40.             progressDialog.setIndeterminate(false);  
  41.             //设置进度条是否按返回键取消   
  42.             progressDialog.setCancelable(true);  
  43.             progressDialog.setButton("确定"new DialogInterface.OnClickListener() {  
  44.                 @Override  
  45.                 public void onClick(DialogInterface dialog, int which) {  
  46.                     progressDialog.cancel();  
  47.                 }  
  48.             });  
  49.             progressDialog.show();  
  50.         }  
  51.     };  
  52.     /** 
  53.      * 显示长形进度条 
  54.      */  
  55.     private View.OnClickListener btnLongOnClickListener=new View.OnClickListener() {  
  56.         int count=0;  
  57.           
  58.         @Override  
  59.         public void onClick(View v) {  
  60.             progressDialog=new ProgressDialog(MainActivity.this);  
  61.             progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  62.             progressDialog.setTitle("提示");  
  63.             progressDialog.setMessage("数据加载中,请稍后....");  
  64.             progressDialog.setIcon(R.drawable.android);  
  65.             progressDialog.setIndeterminate(false);  
  66.             progressDialog.setCancelable(true);  
  67.             progressDialog.setMax(200);  //设置最大值   
  68.             progressDialog.setProgress(0);  //设置当前默认进度为0   
  69.             progressDialog.setSecondaryProgress(100);  //设置第二进度条的值为100   
  70.             progressDialog.setButton("取消"new DialogInterface.OnClickListener() {  
  71.                 @Override  
  72.                 public void onClick(DialogInterface dialog, int which) {  
  73.                     progressDialog.cancel();  
  74.                 }  
  75.             });  
  76.             progressDialog.show();  
  77.               
  78.             //定义线程动态改变当前进度条的值   
  79.             new Thread(){     
  80.                 public void run(){  
  81.                     while(count<=200){  
  82.                         progressDialog.setProgress(count++);  
  83.                         try {  
  84.                             Thread.sleep(100);  
  85.                         } catch (InterruptedException e) {  
  86.                             e.printStackTrace();  
  87.                         }  
  88.                     };  
  89.                 }  
  90.             }.start();  
  91.         }  
  92.     };  
  93. }  

效果如下:

Android组件 稍等提示(ProgressDialog)_第1张图片Android组件 稍等提示(ProgressDialog)_第2张图片

你可能感兴趣的:(java,html,android,layout,button,encoding)