public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // 所有对话框记得要加.show()方法,否则不会显示出对话框 // 普通对话框,(警告对话框 、提示对话框) public void alert(View view) { // 可以在监听里面实现自己的一些功能 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); builder.setIcon(R.drawable.ic_launcher); builder.setMessage("您真的要退出?"); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT) .show();// 没有实现功能,只是点击了提示一下 } }); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT) .show(); } }); builder.setNeutralButton("中立", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "中立", Toast.LENGTH_SHORT) .show(); } }); builder.show(); } // 进度对话框 // 所有对话框记得要加.show()方法,否则不会显示出对话框 public void progress(View view) { ProgressDialog dialog = new ProgressDialog(this); dialog.setTitle("提示"); dialog.setIcon(R.drawable.ic_launcher); dialog.setMessage("正在下载"); dialog.show();// 弹出一个对话框,显示一个去圈圈在加载 } // 日期对话框,可以选择日期,当前日期 public void date(View view) { Calendar calendar = Calendar.getInstance();// 获取当前时间 int year= calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); DatePickerDialog date = new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // 月要加一,因为是从0开始 String datetime = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth; Toast.makeText(MainActivity.this, datetime, Toast.LENGTH_SHORT).show(); } }, year, month, day); date.show(); } // 时间对话框 public void time(View view) { Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MILLISECOND); TimePickerDialog time = new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String time = hourOfDay + "-" + minute; Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show(); } }, hour, minute, true); time.show(); } // 自定义对话框,对话框中可以存放自己的布局控件 public void custom(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("用户登录"); builder.setIcon(R.drawable.ic_launcher); // 填充自定义的登录视图 View loginView = LayoutInflater.from(this).inflate(R.layout.item, null); final TextView txet_name = (TextView) loginView .findViewById(R.id.text_name); final TextView txet_pass = (TextView) loginView .findViewById(R.id.text_pass); builder.setView(loginView); // 添加一个确定按钮 builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String name = txet_name.getText().toString(); String pass = txet_pass.getText().toString(); setTitle(name + ":" + pass); } }); builder.setNegativeButton("取消", null); builder.show(); }}自定义对话框的自定义布局