Android---Dialog 对话框详解

Dialog:对话框

Dialog:是创建对话框的基类。通常不应该直接实例化一个对话框。应该使用他的一个子类。有下面几种子类

  1. AlertDialog警告对话框:它可以管理按钮、单选或者多选的列表
  2. ProgressDialog进度条对话框:他是AlertDialog的扩展,所以也支持按钮
  3. DatePickerDialog日历对话框:可以选择日历
  4. TimePickerDialog时间对话框:可以选择时间

1、AlertDialog:警告对话框

通常在你的Activity的onCreateDialog(int)回调方法中创建对话框。当你使用这个回调函数时,Android系统自动管理每个对话框的状态,并将它们挂钩到Activity上,有效地使它成为每个对话框的“所有者”。因此,每个对话框都继承了活动的某些属性。

  1. 提示对话框
    效果图如下:
    Android---Dialog 对话框详解_第1张图片
    代码如下:
<Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="弹出对话框" />
public class MainActivity extends AppCompatActivity {
     
    private final String TAG = "MainActivity";
    private Button DialogBtn;
    private AlertDialog.Builder alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DialogBtn = findViewById(R.id.button);
        DialogBtn.setOnClickListener(new View.OnClickListener() {
     
            @Override
            public void onClick(View v) {
     
                alertDialog = new AlertDialog.Builder(MainActivity.this);
                //标题
                alertDialog.setTitle("友情提示");
                //内容
                alertDialog.setMessage("今天降温啦,记得加衣哦!");
                //取消按钮
                alertDialog.setNegativeButton("不加", new DialogInterface.OnClickListener() {
     
                    //取消按钮点击时间
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
     
                        Log.i(TAG,"------>取消");
                    }
                });
                //确定按钮
                alertDialog.setPositiveButton("知道啦", new DialogInterface.OnClickListener() {
     
                    //确定按钮点击时间
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
     
                        Log.i(TAG,"------>确定");
                    }
                });
                //忽略按钮
                alertDialog.setNeutralButton("不听不听", new DialogInterface.OnClickListener() {
     
                    //忽略按钮点击时间
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
     
                        Log.i(TAG,"------>确定");
                    }
                });

                alertDialog.show();
            }
        });
    }
}
  1. 单选、多选列表
    效果图如下:
    Android---Dialog 对话框详解_第2张图片
    代码如下
<Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选列表" />
public class MainActivity extends AppCompatActivity {
     
    private final String TAG = "MainActivity";
    private Button DialogBtn;
    private AlertDialog.Builder alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DialogBtn = findViewById(R.id.button2);
        DialogBtn.setOnClickListener(new View.OnClickListener() {
     
            @Override
            public void onClick(View v) {
     
                alertDialog = new AlertDialog.Builder(MainActivity.this);
                //标题
                alertDialog.setTitle("情绪选项");
                final String[] str = {
     "开心","快乐","悲伤"};
                /*
                * str:多选的内容
                * boolean:默认是否选中
                * Listener:选中后可以执行的方法
                * */
                alertDialog.setMultiChoiceItems(str, new boolean[]{
     true, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
     
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
     
                        Log.i(TAG,"---->"+str[which]);

                    }
                });
                //取消按钮
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
     
                    //取消按钮点击时间
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
     
                        Log.i(TAG,"------>取消");
                    }
                });
                //确定按钮
                alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
     
                    //确定按钮点击时间
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
     
                        Log.i(TAG,"------>确定");
                    }
                });

                alertDialog.show();
            }
        });
    }
}

2、ProgressDialog:进度条对话框

效果图如下:

Android---Dialog 对话框详解_第3张图片 Android---Dialog 对话框详解_第4张图片

代码如下:

<Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="圆形进度对话框" />

    <Button
        android:id="@+id/button6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="横向进度对话框" />
public class MainActivity extends AppCompatActivity {
     
    private final String TAG = "MainActivity";
    private Button horDialogBtn,cycleDialogBtn;
    private ProgressDialog progressDialog,progressDialog2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cycleDialogBtn = findViewById(R.id.button5);
        horDialogBtn = findViewById(R.id.button6);



        //按钮触发事件
        cycleDialogBtn.setOnClickListener(new View.OnClickListener() {
     
            @Override
            public void onClick(View v) {
     
                getCycProgressDialog();
            }
        });

        horDialogBtn.setOnClickListener(new View.OnClickListener() {
     
            @Override
            public void onClick(View v) {
     
                getHorProgressDialog();
            }
        });
    }

    public void getCycProgressDialog(){
     
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("友情提示");
        progressDialog.setMessage("正在下载");
        progressDialog.setCancelable(true);//设置点击空白处是否可以取消
        progressDialog.show();
    }

    public void getHorProgressDialog(){
     
        progressDialog2 = new ProgressDialog(this);
        progressDialog2.setTitle("友情提示");
        progressDialog2.setMessage("正在下载");
        progressDialog2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置样式
        progressDialog2.setSecondaryProgress(20);//设置进度条
        progressDialog2.show();
    }
}

3、DatePickerDialog:日历对话框

效果图如下:
Android---Dialog 对话框详解_第5张图片
代码如下:

<TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="日期对话框" />
public class MainActivity extends AppCompatActivity {
     
    private final String TAG = "MainActivity";
    private Button DialogBtn;
    private DatePickerDialog dataDialog;
    private int year,monthOfYear,dayOfMoth;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DialogBtn = findViewById(R.id.button3);
        textView = findViewById(R.id.textView);
        //获取日历对话框
        getDateDialog();

        //按钮触发事件
        DialogBtn.setOnClickListener(
                new View.OnClickListener() {
     
            @Override
            public void onClick(View v) {
     
                dataDialog.show();
            }
        });
    }

	//获取日历对话框
    public void getDateDialog() {
     
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        monthOfYear = calendar.get(Calendar.MONTH);
        dayOfMoth = calendar.get(Calendar.DAY_OF_MONTH);

        //创建一个日历窗口
        dataDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
     
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
     
                textView.setText(year+"年"+(month+1)+"月"+dayOfMonth+"日");
            }
        },year,monthOfYear,dayOfMoth);
    }
}

4、TimePickerDialog:时间对话框

效果图如下:
Android---Dialog 对话框详解_第6张图片

代码如下:

<Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="时间对话框" />
public class MainActivity extends AppCompatActivity {
     
    private final String TAG = "MainActivity";
    private Button DialogBtn;
    private TimePickerDialog timeDialog;
    private int hourOfDay,minute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DialogBtn = findViewById(R.id.button4);
        textView = findViewById(R.id.textView);
        //获取时间对话框
        getTimeDialog();

        //按钮触发事件
        DialogBtn.setOnClickListener(new View.OnClickListener() {
     
            @Override
            public void onClick(View v) {
     
                timeDialog.show();
            }
        });
    }

    //获取时间对话框
    public void getTimeDialog() {
     
        Calendar calendar = Calendar.getInstance();
        hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);

        //创建一个日历窗口
        timeDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
     
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
     
                Log.i(TAG,"--->"+hourOfDay+"时"+minute);
            }
        }, hourOfDay, minute, true);
    }
}

你可能感兴趣的:(Android,android,java)