Defry

博客园 首页 新随笔 联系 订阅 管理

public class DialogActivity extends Activity {

  //进度对话框
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Dialog dialog = new AlertDialog.Builder(DialogActivity.this)
                .setTitle("登录提示")
                // 设置标题
                .setMessage("这里需要登录!")
                // 设置内容
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // 点击确定转向登录框
                        LayoutInflater layoutInflater = LayoutInflater
                                .from(DialogActivity.this);
                        // 得到自定义对话框
                        final View dialogView = layoutInflater.inflate(
                                R.layout.dialog_login, null);
                        // 创建对话框
                        AlertDialog dlg = new AlertDialog.Builder(
                                DialogActivity.this).setTitle("登录框")
                                .setView(dialogView)
                                // 设置自定义对话框的的样式
                                .setPositiveButton("确定",// 设置确定按钮
                                        new DialogInterface.OnClickListener() {// 设置确定按钮事件监听
                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int whichButton) {
                                                // 输入完成,点击确定按钮开始登录,显示进度对话框
                                                progressDialog = ProgressDialog
                                                        .show(DialogActivity.this,
                                                                "请等待……",
                                                                "正在为你登录……",
                                                                true);
                                                new Thread() {
                                                    public void run() {
                                                        try {
                                                            sleep(3000);
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        } finally {
                                                            // 登录结束,取消进度对话框
                                                            progressDialog
                                                                    .dismiss();
                                                            DialogActivity.this.finish();//<自己加的>
                                                        }
                                                    }
                                                }.start();
                                            }
                                        }).setNegativeButton("取消",// 设置取消按钮
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int whichButton) {
                                                // 点击取消按钮退出应用程序
                                                DialogActivity.this.finish();

                                            }
                                        }).create();
                        dlg.show();
                    }
                })

                .setNeutralButton("退出", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // 点击退出按钮,退出应用程序
                        DialogActivity.this.finish();
                    }
                }).create();// 创建按钮
        // 显示对话框
        dialog.show();
    }

}

posted on 2015-04-01 17:37  Defry  阅读(138)  评论(0编辑  收藏  举报