对话框
普通对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
builder.setIcon(R.drawable.a);
builder.setMessage("内容信息");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消 ", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
单选对话框
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setTitle("请选择");
builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
}
});
builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消 ", Toast.LENGTH_SHORT).show();
}
});
final String[] strs={"鸡蛋","鸭蛋","卤蛋 "};
builder1.setSingleChoiceItems(strs, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, strs[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog1 = builder1.create();
alertDialog1.show();
多选框
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setTitle("这是一道多选题");
final String[] items={"1","2","3","4"};
boolean[] flags={true,false,false,false};
builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定", 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.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked){
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
}
});
AlertDialog alertDialog2 = builder2.create();
alertDialog2.show();
自定义对话框
final AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
View inflate = LayoutInflater.from(this).inflate(R.layout.coustom_one, null);
View viewById = inflate.findViewById(R.id.img);
builder3.setView(inflate);
final AlertDialog alertDialog3 = builder3.create();
viewById.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog3.dismiss();
}
});
alertDialog3.show();
水平进度条对话框
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setMessage("正在下载");
progressDialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
if (progress == 100){
progressDialog.dismiss();
timer.cancel();
}
progressDialog.setProgress(progress+=20);
}
},0,1000);
圆形模糊进度对话框
final ProgressDialog progressDialog1 = new ProgressDialog(this);
progressDialog1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog1.setMax(100);
progressDialog1.setMessage("正在下载中...");
progressDialog1.show();
final Timer timer1 = new Timer();
timer1.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
if (progress == 100){
progressDialog1.dismiss();
timer1.cancel();
}
progressDialog1.setProgress(progress+=20);
}
},0,1000);
日期选择对话框
Calendar calendar = 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();
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
时间选择对话框
Calendar calendar1 = 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();
}
},calendar1.get(Calendar.HOUR),calendar1.get(Calendar.MINUTE),false).show();
对话框
创建一个继承Dialog的类
public class MyDialog extends Dialog {
private TextView title;
private TextView message;
private Button no;
private Button yes;
private String Mytitle;
private String Mymessage;
private String Myno;
private String Myyes;
private YesOnClick yesOnClick;
public interface YesOnClick{
void onClick();
}
public void setYesOnClick(YesOnClick yesOnClick) {
this.yesOnClick = yesOnClick;
}
public interface NoOnClick{
void onClick();
}
private NoOnClick noOnClick;
public void setNoOnClick(NoOnClick noOnClick) {
this.noOnClick = noOnClick;
}
public MyDialog(@NonNull Context context) {
super(context);
}
public void setMymessage(String mymessage) {
Mymessage = mymessage;
}
public void setMyno(String myno) {
Myno = myno;
}
public void setMyyes(String myyes) {
Myyes = myyes;
}
public void setMytitle(String mytitle) {
Mytitle = mytitle;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
title = (TextView) findViewById(R.id.title);
if (Mytitle != null){
title.setText(Mytitle);
}
message = (TextView) findViewById(R.id.message);
if (Mymessage != null){
message.setText(Mymessage);
}
no = (Button) findViewById(R.id.no);
if (Myno != null){
no.setText(Myno);
}
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noOnClick.onClick();
dismiss();
}
});
yes = (Button) findViewById(R.id.yes);
if (Myyes != null){
yes.setText(Myyes);
}
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yesOnClick.onClick();
dismiss();
}
});
}
}
操作
MyDialog myDialog = new MyDialog(this);
myDialog.setMytitle("卤蛋头");
myDialog.setMymessage("想学腌制卤蛋的技巧么?");
myDialog.setMyyes("想");
myDialog.setMyno("不想");
myDialog.setNoOnClick(new MyDialog.NoOnClick() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "回答想", Toast.LENGTH_SHORT).show();
}
});
myDialog.setYesOnClick(new MyDialog.YesOnClick() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "想也不教", Toast.LENGTH_SHORT).show();
}
});
myDialog.show();