安卓开发_使用AlertDialog实现对话框

示例:

一、确定对话框

 1 AlertDialog.Builder builder = new AlertDialog.Builder(this);

 2             builder.setTitle("确认对话框");

 3             builder.setIcon(R.drawable.icon_72);

 4             builder.setMessage("这里是对话框内容");

 5             builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

 6                 

 7                 @Override

 8                 public void onClick(DialogInterface arg0, int arg1) {

 9                     // TODO Auto-generated method stub

10                     Toast.makeText(AlertDialog_text.this, "点击了确定按钮", 1).show();

11                 }

12             });

13             AlertDialog dialog = builder.create();

14             dialog.show(); //显示、
View Code

安卓开发_使用AlertDialog实现对话框

二、普通列表

 1 final String[] items = new String[]{"语文","数学","英语","物理","化学"};   //列表项

 2             Builder alertdialog = new AlertDialog.Builder(this); 

 3             alertdialog.setTitle("你喜欢的课程").setItems(items, new DialogInterface.OnClickListener() {

 4                 

 5                 @Override

 6                 public void onClick(DialogInterface dialog, int which) {

 7                     // TODO Auto-generated method stub

 8                     Toast.makeText(AlertDialog_lianxi.this, items[which], Toast.LENGTH_SHORT).show();

 9                     

10                 }

11             });

12             alertdialog.create().show();                  //创建显示列表
普通列表

安卓开发_使用AlertDialog实现对话框

 

三、单选列表

 1 final String[] items_fruit = new String[]{"苹果","香蕉","橘子","西瓜",""};

 2             Builder alerdialog = new AlertDialog.Builder(this);

 3             //设置列表标题

 4             alerdialog.setTitle("你喜欢的水果");

 5             //设置单选列表

 6             alerdialog.setSingleChoiceItems(items_fruit, 0, new DialogInterface.OnClickListener() {

 7                 @Override

 8                 public void onClick(DialogInterface dialog, int which) {

 9                     // TODO Auto-generated method stub

10                     Toast.makeText(AlertDialog_lianxi.this, items_fruit[which], Toast.LENGTH_SHORT).show();

11                     

12                 }

13             });

14             //设置取消按钮并且设置响应事件

15             alerdialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {

16                 

17                 @Override

18                 public void onClick(DialogInterface dialog, int which) {

19                     // TODO Auto-generated method stub

20                     //取消按钮响应事件

21                 }

22             });

23             //添加确定按钮 并且设置响应事件

24             alerdialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {     

25                 

26                 @Override

27                 public void onClick(DialogInterface dialog, int which) {

28                     // TODO Auto-generated method stub

29                     //确定按钮响应事件

30                 }

31             });

32             

33             alerdialog.create().show();//创建显示列表
单选列表

安卓开发_使用AlertDialog实现对话框

四、多选列表

 1 final String[] items_fruit1 = new String[]{"苹果","香蕉","橘子","西瓜",""}; //设置项

 2             final boolean[] items_fruit_selected = new boolean[]{true,false,false,false,false};  

 3             Builder alerdialog1 = new AlertDialog.Builder(this);

 4             //设置列表标题

 5             alerdialog1.setTitle("你喜欢的水果");

 6             //设置多选列表

 7             alerdialog1.setMultiChoiceItems(items_fruit1, items_fruit_selected, new DialogInterface.OnMultiChoiceClickListener() {

 8                 

 9                 @Override

10                 public void onClick(DialogInterface dialog, int which, boolean isChecked) {

11                     // TODO Auto-generated method stub

12                     items_fruit_selected[which] = isChecked;

13                 }

14             });

15             //设置取消按钮并且设置响应事件

16             alerdialog1.setNegativeButton("取消", new DialogInterface.OnClickListener() {

17                 

18                 @Override

19                 public void onClick(DialogInterface dialog, int which) {

20                     // TODO Auto-generated method stub

21                     //取消按钮响应事件

22                 }

23             });

24             //添加确定按钮 并且设置响应事件,将选择的项显示

25             alerdialog1.setPositiveButton("确定", new DialogInterface.OnClickListener() {     

26                 

27                 @Override

28                 public void onClick(DialogInterface dialog, int which) {

29                     // TODO Auto-generated method stub

30                     //确定按钮响应事件

31                     StringBuilder stringBuilder = new StringBuilder();

32                     for(int i=0;i<items_fruit_selected.length;i++)

33                     {

34                         if(items_fruit_selected[i] == true)

35                         {

36                             stringBuilder.append(items_fruit1[i]+"");

37                         }

38                     }

39                     Toast.makeText(AlertDialog_lianxi.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();

40                 }

41             });

42             

43             alerdialog1.create().show();//创建显示列表
多选列表

安卓开发_使用AlertDialog实现对话框

 

安卓开发_使用AlertDialog实现对话框

你可能感兴趣的:(AlertDialog)