首页 > 代码库 > Android 常见对话框

Android 常见对话框

下面代码写了常见的几个对话框,包含:

1、确定取消对话框

2、单选对话框

3、多选对话框

4、进度对话框(加载时的)

5、下载进度对话框

package com.yuanlp.dialogs;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    /**
     * 创建一个确定取消对话框
     * @param view
     */
    public void click1(View view){
        //工厂设计模式,获取对话框工厂
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("警告");
        builder.setMessage("欲练此功,必先自宫");
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"点击了是",Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"不自宫,不成功",Toast.LENGTH_SHORT).show();
            }
        });

        //一定要显示show,不然不会显示
        builder.show();
    }

    /**
     * 创建一个单选对话框
     * @param view
     */
    public void click2(View view){
        //工厂设计模式,获取对话框工厂
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择姓名");
        final String[] items=new String[]{"男","女","未知"};
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"选择的性别是:"+items[which],Toast.LENGTH_LONG).show();
                //关闭对话框
                dialog.dismiss();
            }
        });

        //一定要显示show,不然不会显示
        builder.show();
    }

    /**
     * 创建一个多选对话框
     * @param view
     */
    public void click3(View view){
        //工厂设计模式,获取对话框工厂
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("警告");
        final String[] items={"苹果","香蕉","梨子","桃子"};
        final boolean[] checkdes={false,true,true,true};

        //参数解析:第一个是多选项的一个集合,第二个是每个选项默认的选择状态,第三个是监听
        builder.setMultiChoiceItems(items, checkdes, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                Toast.makeText(MainActivity.this,items[which]+isChecked,Toast.LENGTH_SHORT).show();
            }
        });

        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                StringBuffer sb=new StringBuffer();
                for(int i=0;i<checkdes.length;i++){
                    if (checkdes[i]){
                        sb.append(items[i]+" ");
                    }
                }

                Toast.makeText(MainActivity.this,"喜欢吃的水果是:"+sb.toString(),Toast.LENGTH_SHORT).show();
            }
        });
        //一定要显示show,不然不会显示
        builder.show();
    }

    /**
     * 创建一个进度对话框
     * @param view
     */
    public void click4(View view){
        //工厂设计模式,获取对话框工厂
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setTitle("稍等");
        pd.setMessage("正在拼命加载中");

        //一定要显示show,不然不会显示
        pd.show();

       new Thread(){
           @Override
           public void run() {
               try {
                   Thread.sleep(20000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               pd.dismiss();  //关闭对话框
           }

       }.start();

    }

    /**
     * 创建具体进度条的对话框
     * @param view
     */
    public void click5(View view){
        //工厂设计模式,获取对话框工厂
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setTitle("请等待");
        pd.setMessage("正在拼命加载");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMax(100);
        //一定要显示show,不然不会显示
        pd.show();

        new Thread(){
            @Override
            public void run() {
                try {

                    for (int i=0;i<100;i++){
                        Thread.sleep(300);
                        pd.setProgress(i);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                pd.dismiss();
            }
        }.start();
    }
}


本文出自 “YuanGuShi” 博客,请务必保留此出处http://cm0425.blog.51cto.com/10819451/1940215

Android 常见对话框