Dialog对话框

一、普通的Dialog

    private void showDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setTitle("显示信息标题");
        dialog.setMessage("显示信息内容");
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.show();

    }

二、列表Dialog

    private void showDialog() {
        String[] items = {"张三", "李四", "王五", "赵六"};
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setTitle("显示信息标题");
        dialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.show();
    }

三、单选Dialog

    private void showDialog() {
        String[] items = {"张三", "李四", "王五", "赵六"};
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setTitle("显示信息标题");
        dialog.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.show();
    }

四、多选Dialog

    private void showDialog() {
        String[] items = {"张三", "李四", "王五", "赵六"};
        boolean initChoiceSets[] = {false, false, false, false};
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setTitle("显示信息标题");
        dialog.setMultiChoiceItems(items, initChoiceSets, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {

            }
        });
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.show();
    }

五、等到Dialog

    private void showDialog() {
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setTitle("显示信息标题");
        dialog.setMessage("等待中...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }

六、进度条Dialog

    private void showDialog() {
        final int MAX_PROGRESS = 100;
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setTitle("显示信息标题");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(MAX_PROGRESS);
        dialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int progress = 0;
                while (progress < MAX_PROGRESS) {
                    try {
                        Thread.sleep(100);
                        progress++;
                        dialog.setProgress(progress);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                dialog.cancel();
            }
        }).start();
    }

七、输入框Dialog

    private void showDialog() {
        EditText editText = new EditText(this);
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("标题");
        dialog.setView(editText);
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        dialog.show();
    }

八、自定义Dialog

public class SelfDialog extends Dialog {

    public SelfDialog(Context context) {
        super(context, R.style.SelfDialog);
    }

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



    

        

        

        

        
    


你可能感兴趣的:(Dialog对话框)