Dialog
![](https://i-blog.csdnimg.cn/blog_migrate/45fc6807d86405ea9bb72020906efd2d.png)
public void Dialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Holle Word");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("你好世界");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您点击了Ok", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了cancel", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
居中并且带尺寸的图片:
public void Single_Dialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Holle Word");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了ok", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了cancel", Toast.LENGTH_SHORT).show();
}
});
final String[] items={"篮球","足球","羽毛球"};
builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了是"+items[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public void Mu_Dialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Holle Woed");
builder.setIcon(R.mipmap.ic_launcher);
final String[] items={"排球","足球","篮球"};
final boolean[] flag={true,true,false};
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder sb = new StringBuilder();
for (int i=0;i<flag.length;i++){
if (flag[i]){
sb.append(items[i]+" ");
}
}
Toast.makeText(MainActivity.this, "你点击了"+sb.toString(), Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了cancel", Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(items, flag, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flag[which]=isChecked;
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public void Date_Dialog(){
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+"月"+dayOfMonth+"日", Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.YEAR),instance.get(Calendar.MONTH),instance.get(Calendar.DAY_OF_MONTH)).show();
}
public void Time_Dialog(){
Calendar instance = Calendar.getInstance();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.HOUR),instance.get(Calendar.MINUTE),true).show();
}
public void Hor_Dialog(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.show();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int i=0;
@Override
public void run() {
if (i==100){
progressDialog.dismiss();
}
progressDialog.setProgress(i+=20);
}
},0,1000);
}