- 1
- new AlertDialog.Builder(self)
- 2
- .setTitle("标题" )
- 3
- .setMessage("简单消息框" )
- 4
- .setPositiveButton("确定" , null )
- 5
- .show();
-
- new AlertDialog.Builder(self)
- .setTitle("确认" )
- .setMessage("确定吗?" )
- .setPositiveButton("是" , null )
- .setNegativeButton("否" , null)
- .show();
注意到,这里有两个null参数,这里要放的其实是这两个按钮点击的监听程序,由于我们这里不需要监听这些动作,所以传入null值简单忽略掉,但是实际开发的时候一般都是需要传入监听器的,用来响应用户的操作。
下面是一个可以输入文本的对话框
-
- new AlertDialog.Builder(self)
- .setTitle("请输入" )
- .setIcon(android.R.drawable.ic_dialog_info)
- .setView(new EditText(self))
- .setPositiveButton("确定" , null)
- .setNegativeButton("取消" , null )
- .show();
-
- new AlertDialog.Builder(self)
- .setTitle("请选择" )
- .setIcon(android.R.drawable.ic_dialog_info)
- .setSingleChoiceItems(new String[] {"选项1", "选项2", "选项3" , "选项4" }, 0 ,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- }
- )
- .setNegativeButton("取消" , null )
- .show();
-
- new AlertDialog.Builder(self)
- .setTitle("多选框" )
- .setMultiChoiceItems(new String[] {"选项1", "选项2", "选项3" , "选项4" }, null , null )
- .setPositiveButton("确定" , null)
- .setNegativeButton("取消" , null )
- .show();
-
- new AlertDialog.Builder(self)
- .setTitle("列表框" )
- .setItems(new String[] {"列表项1", "列表项2", "列表项3" }, null )
- .setNegativeButton("确定" , null )
- .show();
-
- ImageView img = new ImageView(self);
- img.setImageResource(R.drawable.icon);
- new AlertDialog.Builder(self)
- .setTitle("图片框" )
- .setView(img)
- .setPositiveButton("确定" , null )
- .show();