Android Dialog5种形式

主页面

Android Dialog5种形式

<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showDA"
        android:text="显示一般AlertDialog" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showLD"
        android:text="显示单选列表AlertDialog" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showCD"
        android:text="显示自定义AlertDialog" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showPD"
        android:text="显示圆形进度ProgressDialog" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showPD2"
        android:text="显示水平进度ProgressDialog"  />

    <Button
        android:id="@+id/Button03"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>


显示一般AlertDialog

Android Dialog5种形式


public void showDA(View view) {
        new AlertDialog.Builder(this).setTitle("删除数据").setMessage("你确定要删除数据吗?")
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this, "删除数据", 0).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this, "取消删除数据", 0).show();
                    }
                }).show();
    }



显示单选列表AlertDialog

Android Dialog5种形式

public void showLD(View view) {
        final String[] items = { "红", "黄", "蓝", "绿" };
        new AlertDialog.Builder(this)
                .setTitle("确定指定颜色")
                .setSingleChoiceItems(items, items.length,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // 显示信息
                                Toast.makeText(MainActivity.this, items[which],
                                        0).show();
                                // 移除
                                dialog.dismiss();
                            }
                        }).show();
    }



显示自定义AlertDialog

Android Dialog5种形式

public void showCD(View v) {
        // 动态加载布局文件
        View view = View.inflate(this, R.layout.zidingyi, null);
        // 问题1。view的真实类型?布局文件跟标签的类型
        // 问题2。如何得到一个独立View的子view view.findViewById()
        final EditText name = (EditText) view.findViewById(R.id.name);
        final EditText pass = (EditText) view.findViewById(R.id.pass);

        new AlertDialog.Builder(this).setView(view)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 读取用户名,密码
                        String username = name.getText().toString();
                        String password = pass.getText().toString();
                        // 提示
                        Toast.makeText(MainActivity.this,
                                username + ":" + password, 0).show();
                    }
                }).show();
    }


显示圆形进度ProgressDialog

Android Dialog5种形式

public void showPD(View v) {
        final ProgressDialog dialog = ProgressDialog.show(this, "数据加载",
                "数据加载中...");

        // 长时间的工作不能再主线程执行,要在分线程执行
        new Thread() {
            public void run() {
                for (int i = 0; i < 50; i++) {
                    // 休息一下
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                dialog.dismiss();
            };
        }.start();
    }


显示水平进度ProgressDialog

Android Dialog5种形式

public void showPD2(View v) {

        // 创建Dialog对象
        final ProgressDialog dialog = new ProgressDialog(this);
        // 设置样式
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // 显示
        dialog.show();

        // 启动分线程加载数据并显示进度当加载完成移除Dialog
        new Thread(new Runnable() {

            @Override
            public void run() {
                int count = 100;
                dialog.setMax(count);
                for (int i = 0; i < count; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    dialog.setProgress(dialog.getProgress() + 1);
                }
                // 移除Dialog
                dialog.dismiss();
            }
        }).start();
    }