灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2714回复:0

Android 程式开发:(一)详解活动 —— 1.6 显示复杂对话框

楼主#
更多 发布于:2012-09-06 14:03

除了“对话框”进度条,也可以创建一个显示“操作进度”的对话框,例如显示正在下载的状态。
1、创建一个工程:Dialog。

2、main.xml中的代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:orientation="vertical" >

    <Button
        Android:id="@+id/btn_dialog3"
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content"
        Android:onClick="onClick3"
        Android:text="Click to display a detailed progress dialog" />

</LinearLayout>
3、DialogActivity.java中的代码。package net.horsttnann.Dialog;

import Android.app.Activity;
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 {
    ProgressDialog progressDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onClick3(View v) {
        showDialog(1);
        progressDialog.setProgress(0);

        new Thread(new Runnable() {
            public void run() {
                for (int i = 1; i <= 15; i++) {
                    try {
                        // ---simulate doing something lengthy---
                        Thread.sleep(1000);
                        // ---update the dialog---
                        progressDialog.incrementProgressBy((int) (100 / 15));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                progressDialog.dismiss();
            }
        }).start();
    }
www.atcpu.com
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 1:
            progressDialog = new ProgressDialog(this);
            progressDialog.setIcon(R.drawable.ic_launcher);
            progressDialog.setTitle("Downloading files...");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            Toast.makeText(getBaseContext(), "OK clicked!",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            Toast.makeText(getBaseContext(), "Cancel clicked!",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            return progressDialog;
        }
        return null;
    }
}
4、按F11调试。

  





想要创建一个显示进度的对话框,首先要创建一个ProgressDialog类的实例,然后设置各种状态,图标、标题、样式等等:

progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("Downloading files...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
然后设置两个按钮:progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int whichButton) {
                Toast.makeText(getBaseContext(), "OK clicked!",
                        Toast.LENGTH_SHORT).show();
            }
        });
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int whichButton) {
                Toast.makeText(getBaseContext(), "Cancel clicked!",
                        Toast.LENGTH_SHORT).show();
            }
        });
使用一个线程显示进度条的状态:progressDialog.setProgress(0);

new Thread(new Runnable() {
    public void run() {
        for (int i = 1; i <= 15; i++) {
            try {
                // ---simulate doing something lengthy---
                Thread.sleep(1000);
                // ---update the dialog---
                progressDialog.incrementProgressBy((int) (100 / 15));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        progressDialog.dismiss();
    }
}).start();
当进度条到达100%的时候,它就被解除了。

作者 manoel的专栏

喜欢0 评分0
游客

返回顶部