前言
最近有些空时间,所以花了一个小时对Dialog对话框使用小结一下,比较基础,希望对你学习有帮助,大牛请直接关闭网页。如果你是新手,建议你亲自敲一遍代码。
先看一下效果:
Dialog对话框使用小结
一、普通对话框
AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("温馨提示");//标题 builder.setMessage("天气冷,注意保暖"); builder.setIcon(R.mipmap.ic_launcher); builder.create(); builder.show();
普通对话框
二、确定取消对话框
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();
确定取消对话框
三、多按钮对话框
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();
多按钮对话框
四、列表对话框
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();
列表对话框
五、带Adapter的对话框
builder.setTitle("带Adapter的对话框"); builder.setIcon(R.mipmap.ic_launcher); final List
带Adapter的对话框
六、单选对话框
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();
单选对话框
七、多选对话框
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();
多选对话框
八、日期对话框
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();
日期对话框
九、时间对话框
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();
时间对话框
十、自定义对话框
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
<&#63;xml version="1.0" encoding="utf-8"&#63;>
dialog_login.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
源码下载
总结
以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。