1、确定取消对话框
AlertDialog不可创建对象,要用AlertDialog.Builder builder = new Builder(conext);
AlertDialog.Builder builder = new Builder(this); //创建对象 builder.setIcon(android.R.drawable.alert_light_frame ); //设置头像,这里是系统里面的 builder.setTitle("你屌不屌?"); //设置标题 builder.setMessage("叼叼叼!"); //设置内容 builder.setCancelable(false); //不能点返回 builder.setPositiveButton("是的", new OnClickListener(){ @Override //设置确定按钮 ,注意导包import android.content.DialogInterface.OnClickListener; public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "叼叼叼", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("否的", new OnClickListener() { @Override //设置取消按钮 public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "搓搓搓", Toast.LENGTH_SHORT).show(); } }); AlertDialog aler = builder.create(); aler.show(); //show()
Android4.0以上AlertDialog在触摸对话框边缘外部,对话框消失
可以设置这么一条属性,当然必须先AlertDialog.Builder.create()之后才能调用这两个方法
方法一:
setCanceledOnTouchOutside(false);调用这个方法时,按对话框以外的地方不起作用。按返回键还起作用
方法二:
setCanceleable(false); 调用这个方法时,按对话框以外的地方不起作用。按返回键也不起作用
2、单选对话框
<pre name="code" class="java">AlertDialog.Builder builder = new Builder(this); builder.setTitle("你屌不屌?"); final String[] items = new String[]{"男","女"}; builder.setSingleChoiceItems(items, 0/*这里是默认被选中的,不选为-1,第一个为0*/, new OnClickListener(){ @Override //which用户所选的下标 public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选中:"+items[which], 0).show(); dialog.dismiss();//关闭对话框 } }); builder.create().show();
![]()
![]()
![]()
3.多选对话框
AlertDialog.Builder builder = new Builder(this); builder.setTitle("你屌不屌?"); final String[] items = new String[]{"男","女","妖","魔","鬼"}; final boolean[] b = new boolean[]{false,false,false,false,false}; //如果是true就是默认选中 builder.setMultiChoiceItems(items, b, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { b[which] = isChecked; //记录你的选择 } }); builder.setPositiveButton("确定", new OnClickListener() { @Override //设置确定按钮 public void onClick(DialogInterface dialog, int which) { String text = ""; for(int i=0;i<b.length;i++){ text += b[i]?items[i]:""; } Toast.makeText(MainActivity.this, text, 0).show(); dialog.dismiss(); //关闭对话框 } }); builder.create().show();
4、自定义对话框(资源在java函数中,用适配器)
1)layout里面布局xml文件: R.layout.item_dailog2)代码:
final String[] str1 = new String[] { "吊", "不屌", "吊炸天", "有点吊", "66666" }; final int[] icon = new int[] { R.drawable.qb_20, R.drawable.qb_21, R.drawable.qb_22, R.drawable.qb_24, R.drawable.qb_25 }; List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < str1.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", str1[i]); map.put("image", icon[i]); list.add(map); } SimpleAdapter adp = new SimpleAdapter(MainActivity.this, list, R.layout.item_dailog, new String[] { "name", "image" }, new int[] { R.id.tv_item, R.id.iv_item }); builder.setAdapter(adp, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "点击了:" + str1[which], Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.create().show();
![]()
5、自定义对话框(资源来自对话框输入,用布局)
1)layout里面布局xml文件:R.layout.item02_dailong2)代码://泵布局,得到组件 View diog = getLayoutInflater().inflate(R.layout.item02_dailong,null); final EditText name = (EditText) diog.findViewById(R.id.editText1); final EditText pass = (EditText) diog.findViewById(R.id.editText2); //setview builder.setView(diog); //监听,这里设置一个确定键来监听 builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String usrName = name.getText().toString(); String usrPass = pass.getText().toString(); if (usrName.equals("abc") && usrPass.equals("123")) { Toast.makeText(MainActivity.this, "登录成东",Toast.LENGTH_SHORT).show(); } } }); //创建并show出来 builder.create().show();
![]()
6、DatePickerDialog(时间对话框)
//获取当前时间,作为DatePickerDialog的参数 Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int monthOfYear = calendar.get(Calendar.MONTH); int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); //new DatePickerDialog DatePickerDialog dpd = new DatePickerDialog(MainActivity.this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this,year + "-" + monthOfYear + "-" + dayOfMonth,Toast.LENGTH_SHORT).show(); } }, year, monthOfYear, dayOfMonth); //show dpd.show();
![]()