Android攻城狮Dialog

归志宁无五亩园,读书本意在元元。这篇文章主要讲述Android攻城狮Dialog相关的知识,希望能为你提供帮助。
【Android攻城狮Dialog】

Android攻城狮Dialog

文章图片

 

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

 

    推荐阅读