对话框是程序运行中的弹出窗口,例如,当用户要删除一个联系方式时,会弹出一个对话框,让用户确认是否真的要删除。
Android系统提供了多种对话框:
- AlertDialog(警告对话框)
- ProgressDialog(进度条对话框)
- DatePickerDialog(日期选择对话框)
- TimerPickerDialog(时间选择对话框)
- title 标题是可选部分
- Content Area 显示message、list…
- Action Buttons 可以添加操作,不超过三个操作按钮
package com.example.test_alertdialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void test1(View view){
//如何创建一个alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示信息!");
builder.setMessage("我的第一个对话框!");
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
3、对话框添加按钮
包括不同性质的按钮:setPositiveButton、setNegativeButton、setNeutralButton
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void test1(View view){
//如何创建一个alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示信息!");
builder.setMessage("我的第一个对话框!");
builder.setPositiveButton("非常满意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "非常满意", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("不满意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "不满意", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("一般", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "一般", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
public void test2(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示信息!");
builder.setItems(ss, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
5、对话框单项选择
解析:
builder.setSingleChoiceItems(ss, **0**, new DialogInterface.OnClickListener(){}
//其中的0,表示初始点开时选中数组中第0个
代码:
public void test3(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示信息!");
builder.setSingleChoiceItems(ss, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
6、对话框多项选择
解析:
builder.setMultiChoiceItems(ss, null, new DialogInterface.OnMultiChoiceClickListener() {}
//其中的null,表示初始点开时不选中数组中的任意值
//null也可换成new boolean[]{true,false,true},true为选中,false为不选中
代码:
public void test4(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示信息!");
builder.setMultiChoiceItems(ss, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}



