首页 > 代码库 > Android攻城狮Dialog

Android攻城狮Dialog

技术分享

 

 

  1 public class MainActivity extends ActionBarActivity {
  2     String[] single_list = { "男", "女" };
  3     String[] multi_list = { "吃饭", "睡觉", "打豆豆" };
  4     String[] item_list = { "经理", "策划", "测试", "美工" };
  5 
  6     @Override
  7     protected void onCreate(Bundle savedInstanceState) {
  8         super.onCreate(savedInstanceState);
  9         setContentView(R.layout.fragment_main);
 10         initEvent();
 11 
 12     }
 13 
 14     private void initEvent() {
 15         // TODO Auto-generated method stub
 16         findViewById(R.id.button5).setOnClickListener(new OnClickListener() {
 17 
 18             @Override
 19             public void onClick(View v) {
 20                 // TODO Auto-generated method stub
 21                 // showDialog1();
 22                 // showDialog2();
 23                 // showDialog3();
 24                 // showDialog4();
 25                 showDialog5();
 26             }
 27 
 28         });
 29     }
 30 
 31     // 显示自定义对话框
 32     private void showDialog5() {
 33         // TODO Auto-generated method stub
 34         LayoutInflater inflater = LayoutInflater.from(this);
 35         View view = inflater.inflate(R.layout.dialog, null);
 36         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 37         builder.setTitle("自定义对话框");
 38         builder.setIcon(R.drawable.ic_launcher);
 39         builder.setView(view);
 40         AlertDialog dialog = builder.create();
 41         dialog.show();
 42     }
 43 
 44     // 显示列表对话框
 45     private void showDialog4() {
 46         // TODO Auto-generated method stub
 47         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 48         builder.setTitle("部门列表");
 49         builder.setIcon(R.drawable.ic_launcher);
 50         builder.setItems(item_list, new DialogInterface.OnClickListener() {
 51 
 52             @Override
 53             public void onClick(DialogInterface dialog, int which) {
 54                 // TODO Auto-generated method stub
 55                 Toast.makeText(MainActivity.this, "部门是是:" + item_list[which], 0)
 56                         .show();
 57             }
 58         });
 59 
 60         AlertDialog dialog = builder.create();
 61         dialog.show();
 62     }
 63 
 64     // 显示单选按钮对话框
 65     private void showDialog2() {
 66         // TODO Auto-generated method stub
 67         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 68         builder.setTitle("选择性别");
 69         builder.setIcon(R.drawable.ic_launcher);
 70         builder.setSingleChoiceItems(single_list, 0,
 71                 new DialogInterface.OnClickListener() {
 72 
 73                     @Override
 74                     public void onClick(DialogInterface dialog, int which) {
 75                         // TODO Auto-generated method stub
 76                         String str = single_list[which];
 77                         Toast.makeText(MainActivity.this, "这人人是:" + str, 0)
 78                                 .show();
 79                     }
 80                 });
 81 
 82         AlertDialog dialog = builder.create();
 83         dialog.show();
 84     }
 85 
 86     // 显示多选按钮对话框
 87     private void showDialog3() {
 88         // TODO Auto-generated method stub
 89         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 90         builder.setTitle("爱好:");
 91         builder.setIcon(R.drawable.ic_launcher);
 92         builder.setMultiChoiceItems(multi_list, null,
 93                 new DialogInterface.OnMultiChoiceClickListener() {
 94 
 95                     @Override
 96                     public void onClick(DialogInterface dialog, int which,
 97                             boolean isChecked) {
 98                         // TODO Auto-generated method stub
 99                         if (isChecked) {
100                             Toast.makeText(MainActivity.this,
101                                     "爱好是:" + multi_list[which], 0).show();
102                         }
103 
104                     }
105                 });
106         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
107 
108             @Override
109             public void onClick(DialogInterface dialog, int which) {
110                 // TODO Auto-generated method stub
111                 dialog.dismiss();
112             }
113         });
114         AlertDialog dialog = builder.create();
115         dialog.show();
116     }
117 
118     // 显示确认对话框
119     private void showDialog1() {
120         AlertDialog.Builder builder = new AlertDialog.Builder(this);
121         builder.setTitle("确认对话框");
122         builder.setMessage("确认提示对话框内容");
123         builder.setIcon(R.drawable.ic_launcher);
124         builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
125 
126             @Override
127             public void onClick(DialogInterface dialog, int which) {
128                 // TODO Auto-generated method stub
129                 Toast.makeText(MainActivity.this, "点击了确定", 0).show();
130 
131             }
132         });
133         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
134 
135             @Override
136             public void onClick(DialogInterface dialog, int which) {
137                 // TODO Auto-generated method stub
138                 Toast.makeText(MainActivity.this, "点击了取消", 0).show();
139 
140             }
141         });
142         AlertDialog dialog = builder.create();
143         dialog.show();
144     }
145 
146 }

 

Android攻城狮Dialog