Android基础控件——AlertDialog、ProgressDialog实现单选对话框、多选对话框、进度条对话框、输入框对话框

AlertDialog、ProgressDialog实现单选对话框、多选对话框、进度条对话框、输入框对话框


事先说明:

对话框应选用android.support.v7这个包底下的组件,能在低版本中展示高版本的对话框,简单的说漂亮很多

属性介绍:

setCancelable : 设置对话框能否被手动关闭
步骤一:确认对话框
    private void showConfirmDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提醒");
        builder.setMessage("是否确认更新?");
        builder.setPositiveButton("更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

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

            }
        });
        builder.show();
    }
Android基础控件——AlertDialog、ProgressDialog实现单选对话框、多选对话框、进度条对话框、输入框对话框_第1张图片

步骤二:单选对话框
    private void showSingleDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择");
        String[] items = new String[]{"条目一", "条目二", "条目三"};
        //参数一:选择框的条目的值
        //参数二:默认选择条目
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //items[which]被选中
            }
        });
        builder.show();
    }
Android基础控件——AlertDialog、ProgressDialog实现单选对话框、多选对话框、进度条对话框、输入框对话框_第2张图片

步骤三:多选对话框
    private void showMultiDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择");
        String[] items = new String[]{"条目一", "条目二", "条目三"};
        //参数一:选择框的条目的值
        //参数二:默认选择条目
        builder.setMultiChoiceItems(items, new boolean[]{true, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                //items[which]被选中
            }
        });
        builder.show();
    }


步骤四:圆形进度条对话框
    private void showRoundProgressDialog() {
        ProgressDialog pd = new ProgressDialog(this);
        pd.setTitle("更新中");
        pd.setMessage("系统正在升级");
        pd.show();
    }
Android基础控件——AlertDialog、ProgressDialog实现单选对话框、多选对话框、进度条对话框、输入框对话框_第3张图片

步骤五:水平进度条对话框
    private void showHorizontalProgressDialog() {
        ProgressDialog pd = new ProgressDialog(this);
        pd.setTitle("更新中");
        pd.setMessage("系统正在升级");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.show();
    }
Android基础控件——AlertDialog、ProgressDialog实现单选对话框、多选对话框、进度条对话框、输入框对话框_第4张图片

步骤六:输入框对话框
布局文件view_username_dilog.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">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp" />

</LinearLayout>
代码中实现:
    public static void showUserNameDialog() {
        LayoutInflater factory = LayoutInflater.from(this);
        View view = factory.inflate(R.layout.view_username_dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("昵称");
        builder.setView(view);
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {

            }
        });
        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {

            }
        });
        builder.show();
    }
Android基础控件——AlertDialog、ProgressDialog实现单选对话框、多选对话框、进度条对话框、输入框对话框_第5张图片








 
 

你可能感兴趣的:(android,控件,对话框,ProgressDialog,AlertDialog)