AlertDialog 警告对话框

来源:互联网 时间:1970-01-01

AlertDialog 警告对话框

用new AlertDialog.Builder(Context context)来创建一个builder
可以利用builder设置基本信息如 标题、信息、icon:
Builder.setTitle(CharSequence title)
Builder.setMessage(CharSequence message)
Builder.setIcon(int iconId)

还可以创建对话框按钮:
Builder.setPositiveButton(CharSequence text, OnClickListener listener)  //确认按钮

Builder.setNegativeButton(CharSequence text, OnClickListener listener)  //取消按钮

Builder.setNeutralButton(CharSequence text, OnClickListener listener)  //忽略按钮

最后创建一个alertDialog:
AlertDialog alertDialog = builder.create();   //创建Dialog

alertDialog.show(); //让对话框显示

完整代码如下:

<span style="white-space:pre"> </span>public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)this.findViewById(R.id.alertButton1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //对Dialog基本信息的一个构建 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示"); //标题 builder.setMessage("你确定要删除么?"); //信息 builder.setIcon(R.drawable.ic_launcher); //icon //以上也是可以无限.下去 如: //builder.setTitle("提示").setMessage("你确定要删除么?").setIcon(R.drawable.ic_launcher); //确认按钮 builder.setPositiveButton("是",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //完成用户的操作,比如删除数据,提交请求等 Toast.makeText(getApplicationContext(), "你点击了确定按钮",0).show(); dialog.dismiss(); //让对话框消失,其实不用它dismiss()也行,会自动消失 } }); //取消按钮 builder.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //取消用户的操作,比如是停止下载等功能 Toast.makeText(getApplicationContext(), "你点击了取消按钮", 0).show(); } }); //忽略按钮 builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //忽略用户的操作 } }); AlertDialog alertDialog = builder.create(); //创建Dialog alertDialog.show(); //让对话框显示 } }); } }

二、当然如果想要做一个列表选项对话框,就可以使用
Builder.setItems(CharSequence[] items, OnClickListener listener)


完整代码如下:
<span style="white-space:pre"> </span>public class MainActivity extends Activity { private Button ListButton2; //定义 3个item列表选项 private final CharSequence[] items = {"上海","江西","广西","北京"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListButton2 = (Button)this.findViewById(R.id.ListButton2); ListButton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选择以下城市"); // builder.setMessage("请选择以下城市"); //不能设置Message的内容 builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog = builder.create(); dialog.show(); } }); } }


三、如果想要做一个单选列表选项对话框,就可以使用

<span style="white-space:pre"> </span>public class MainActivity extends Activity { private Button radioButton3; //定义 3个item列表选项 private final CharSequence[] items = {"上海","江西","广西","北京"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioButton3 = (Button)this.findViewById(R.id.radioButton3); radioButton3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选择以下城市"); //checkedItem为-1 为初始化时没有选中选项 builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { CharSequence select_item = items[which]; Toast.makeText(MainActivity.this, select_item, 0).show(); dialog.dismiss(); //让对话框消失 } }); AlertDialog dialog = builder.create(); dialog.show(); } }); } }

四、如果想要做一个多选列表选项对话框,就可以使用:
Builder.setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, OnMultiChoiceClickListener listener)


在MainActivity.java中完整代码如下:

<span style="white-space:pre"> </span>public class MainActivity extends Activity { private Button selectButton4; //定义 4个item列表选项 private final CharSequence[] items = {"上海","江西","广西","北京"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); selectButton4 = (Button)this.findViewById(R.id.selectButton4); selectButton4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选择以下城市"); //第二个参数可以设置为Null,表示没有选择默认被选中 //使用StringBuffer 追加,中间用逗号隔开 builder.setMultiChoiceItems(items, new boolean[]{true,true,false,false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { } }); //确认按钮 builder.setPositiveButton("是",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你点击了确定按钮",0).show(); dialog.dismiss(); //让对话框消失,其实不用它dismiss()也行,会自动消失 } }); AlertDialog dialog = builder.create(); dialog.show(); } }); } } 




当然可以不用CharSequence[]引入数据,可以用数组:
在项目中的目录res/strings.xml中定义一个:
<string-array name="city">
<item>上海</item>
<item>江西</item>
<item>广西</item>
<item>北京</item>
</string-array>
在之前的方法中的CharSequence[] items可以改为string-array
用R.array.city引用出来

以上是AlertDialog的算是完整的内容。。。



相关阅读:
Top