大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Android技巧 > android进度对话框的使用

android进度对话框的使用

关键词:对话框进度android  阅读(601) 赞(20)

[摘要]本文是对android进度对话框的使用的讲解,对学习Android编程技术有所帮助,与大家分享。

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:orientation="vertical"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".DialogActivity" >  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/text" />  
  
    <Button   
        android:id="@+id/btn_dialog"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:onClick="onClick"  
        android:text="多选对话框"  
        />  
    <Button   
        android:id="@+id/btn_dialog1"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:onClick="onClick1"  
        android:text="进度对话框"  
        />  
    <Button   
        android:id="@+id/btn_dialog2"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:onClick="onClick2"  
        android:text="详细进度对话框"  
        />  
</LinearLayout>  

DialogActivity.java

package com.dialog;  
  
import android.app.Activity;  
import android.app.AlertDialog;  
import android.app.AlertDialog.Builder;  
import android.app.Dialog;  
import android.app.ProgressDialog;  
import android.content.DialogInterface;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Toast;  
  
public class DialogActivity extends Activity {  
      
    //CharSequence接口  
    CharSequence[] items = {"谷歌","苹果","百度","微软"};  
    boolean[] itemsChecked = new boolean[items.length];  
      
    //创建ProgressDialog类的一个实例  
    ProgressDialog progressDialog = null;   
      
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
    }  
      
  
    //重写onCreateDialog()方法  
    protected Dialog onCreateDialog(int id, Bundle args) {  
        switch (id) {  
        case 0:  
            /* 
             * AlertDialog Dialog的子类 显示进度指示器和可选的文本消息或视图的对话框 
             * 多选对话框 
             */  
            Builder builder = new AlertDialog.Builder(this);  
            builder.setIcon(R.drawable.ic_launcher);  
            builder.setTitle("你喜欢哪家互联网公司");  
              
            //响应确定按钮  
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
                  
                //Toast并没有在DialogActivity直接使用,而是在AlertDialog类中使用  因此需要用getBaseContext()返回一个Context类的实例  
                public void onClick(DialogInterface dialog, int whichButton) {  
                    Toast.makeText(getBaseContext(), "确定按钮被点击", Toast.LENGTH_SHORT).show();  
                }  
            });  
              
            //响应取消按钮  
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){  
  
                public void onClick(DialogInterface dialog, int whichButton) {  
                    Toast.makeText(getBaseContext(), "取消按钮被点击", Toast.LENGTH_SHORT).show();  
                }  
            });  
              
            //响应多选事件  
            builder.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {  
                  
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {  
                    Toast.makeText(getBaseContext(), (isChecked?"选中":"未选中")+items[which], Toast.LENGTH_SHORT).show();  
                }  
            });  
            return builder.create();  
              
        case 1:  
            /* 
             * 设置图标 标题 样式 按钮 
             * 显示详细进度对话框 
             */  
            progressDialog = new ProgressDialog(this);  
            progressDialog.setIcon(R.drawable.ic_launcher);  
            progressDialog.setTitle("下载文件...");  
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
            progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new DialogInterface.OnClickListener() {  
                  
                public void onClick(DialogInterface dialog, int which) {  
                    Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show();  
                }  
            });  
            progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",new DialogInterface.OnClickListener() {  
                  
                public void onClick(DialogInterface dialog, int which) {  
                    Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();  
                }  
            });  
            return progressDialog;  
        }  
          
        return null;  
    }  
      
    public void onClick(View v){  
        //调用onCreateDialog()方法  
        showDialog(0);  
    }  
      
    /* 
     * 进度对话框 
     */  
    public void onClick1(View v){  
        final ProgressDialog dialog = ProgressDialog.show(this, "请等待3秒", "Please wait...",true);  
        //启动一个新线程  
        new Thread(new Runnable() {  
            public void run() {  
                try {  
                    Thread.sleep(3000);  
                    //解除对话框 从屏幕上移除 此方法是线程安全的  
                    dialog.dismiss();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }).start();  
    }  
      
    /* 
     * 详细进度对话框 
     */  
    public void onClick2(View v){  
        showDialog(1);  
        //设置初始值为0  
        progressDialog.setProgress(0);  
          
        new Thread(new Runnable() {  
                public void run(){  
                    for (int i = 1; i <= 15; i++) {  
                        try {  
                            Thread.sleep(1000);  
                            //更新对话框  
                            progressDialog.incrementProgressBy((int)100/15);  
                        } catch (InterruptedException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                    progressDialog.dismiss();  
            }  
        }).start();  
          
    }  
  
}  


相关评论