花了一个小时对Dialog对话框使用小结一下,比较基础,希望对你学习有帮助,大牛请直接关闭网页。如果你是新手,建议你亲自敲一遍代码。
先看一下效果:
1. 普通对话框
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("温馨提示");//标题
builder.setMessage("天气冷,注意保暖");
builder.setIcon(R.mipmap.ic_launcher);
builder.create();
builder.show();
2. 确定取消对话框
builder.setTitle("确定取消对话框");
builder.setMessage("请选择确定或取消");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//正能量按钮 Positive
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "你点击了确定", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "你选择了取消", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
3. 多按钮对话框
builder.setTitle("多个按钮对话框");
builder.setMessage("请选择");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("我没玩够", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "继续浏览精彩内容", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("开启", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "起床了", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("我累了,要休息一下", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "欢迎再来", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
4. 列表对话框
final String arrItem[] = getResources().getStringArray(R.array.aikaifa);
builder.setItems(arrItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "你选择了第" + arrItem[which], Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
5. 带Adapter的对话框
builder.setTitle("带Adapter的对话框");
builder.setIcon(R.mipmap.ic_launcher);
final List
6. 单选对话框
builder.setTitle("单选对话框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setSingleChoiceItems(R.array.aikaifa, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, which+"", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
7. 多选对话框
builder.setTitle("多选对话框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMultiChoiceItems(R.array.aikaifa, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(activity, which+""+isChecked, Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
8. 日期对话框
DatePickerDialog datePickerDialog=new DatePickerDialog(activity,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Toast.makeText(activity,
year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日", Toast.LENGTH_SHORT).show();
}
},
2017, 02, 9);
datePickerDialog.show();
9. 时间对话框
TimePickerDialog timePickerDialog=new TimePickerDialog(activity,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(activity,
hourOfDay+"小时"+minute+"分钟", Toast.LENGTH_SHORT).show();
}
},
17, 49, true);
timePickerDialog.show();
10. 自定义对话框
View view= LayoutInflater.from(activity).inflate(R.layout.dialog_login, null);
builder.setView(view);
builder.create();
final EditText et_phone=(EditText)view.findViewById(R.id.et_phone);
final EditText et_password=(EditText)view.findViewById(R.id.et_password);
Button btn_submit=(Button)view.findViewById(R.id.btn_submit);
btn_submit.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(activity, "手机号码:"+et_phone.getText().toString()+" 短信验证码:"+et_password.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
builder.show();
项目设计到的xml
list_item.xml
dialog_login.xml
源码下载
[END]