//对话框标签
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
builder.setMessage("内容");
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "已取消", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"" , Toast.LENGTH_SHORT).show();
}
});
builder.show();
日期对话框
Calendar instance = Calendar.getInstance();
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.YEAR),instance.get(Calendar.MONTH),instance.get(Calendar.DAY_OF_MONTH)).show();
break;
case R.id.bt_3:
Calendar instance1 = Calendar.getInstance();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay+1+"-"+minute, Toast.LENGTH_SHORT).show();
}
},instance1.get(Calendar.HOUR_OF_DAY),instance1.get(Calendar.MINUTE),true).show();
时间对话框
//获取时间
Calendar instance1 = Calendar.getInstance();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay+1+"-"+minute, Toast.LENGTH_SHORT).show();
}
},instance1.get(Calendar.HOUR_OF_DAY),instance1.get(Calendar.MINUTE),true).show();
自定义对话框
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setTitle("标题");
View inflate = layoutInflater.from(MainActivity.this).inflate(R.layout.main, null);
builder1.setView(inflate);
builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "已取消", Toast.LENGTH_SHORT).show();
}
});
builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"" , Toast.LENGTH_SHORT).show();
}
});
builder1.show();
多选对话框
final String[] strings=new String[]{"乒乓球","篮球","足球","桌球","大气球"};
boolean[] booleans=new boolean[]{false,false,false,false,false};
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setTitle("标题");
builder2.setMultiChoiceItems(strings, booleans, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked)
{
Toast.makeText(MainActivity.this, "您选择了"+strings[which], Toast.LENGTH_SHORT).show();
}
}
});
builder2.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "已取消", Toast.LENGTH_SHORT).show();
}
});
builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"" , Toast.LENGTH_SHORT).show();
}
});
builder2.show();
单选对话框
final String[] string=new String[]{"男","女","不男不女","人妖","女博士"};
AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
builder3.setTitle("标题");
builder3.setSingleChoiceItems(string, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了"+string[which], Toast.LENGTH_SHORT).show();
}
});
builder3.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "已取消", Toast.LENGTH_SHORT).show();
}
});
builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"" , Toast.LENGTH_SHORT).show();
}
});
builder3.show();
进度条对话框
final Timer timer=new Timer();
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("标题");
progressDialog.setProgress(20);
progressDialog.setProgressStyle(50);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
timer.schedule(new TimerTask() {
private int aaa=0;
@Override
public void run() {
aaa+=10;
progressDialog.setProgress(aaa);
if (aaa>100)
{
timer.cancel();
progressDialog.dismiss();
}
}
},0,1000);
progressDialog.show();