资源包:范例练习b9 FLExB9.rar
需求分析:
1.AlertDialog分类:
最简单的set几个button positive neutral negative
可以点击的setitems
单选的singleChoiceItem
复选MultChoiceItem
登录框 通过layoutInflater
View Code
1 package com.example.flexb9; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.app.AlertDialog; 6 import android.content.Context; 7 import android.content.DialogInterface; 8 import android.view.LayoutInflater; 9 import android.view.Menu; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 import android.widget.Toast; 14 15 public class MainActivity extends Activity { 16 17 private Button bt1; 18 private Button bt2; 19 private Button bt3; 20 private Button bt4; 21 private Button bt5; 22 private int count; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 29 bt1 = (Button)findViewById(R.id.bt1); 30 bt1.setOnClickListener(bt1Listener); 31 bt2 = (Button)findViewById(R.id.bt2); 32 bt2.setOnClickListener(bt1Listener); 33 bt3 = (Button)findViewById(R.id.bt3); 34 bt3.setOnClickListener(bt1Listener); 35 bt4 = (Button)findViewById(R.id.bt4); 36 bt4.setOnClickListener(bt1Listener); 37 bt5 = (Button)findViewById(R.id.bt5); 38 bt5.setOnClickListener(bt1Listener); 39 } 40 41 Button.OnClickListener bt1Listener = new Button.OnClickListener(){ 42 @Override 43 public void onClick(View v) { 44 final String[] itemArray = getResources().getStringArray(R.array.dialog_item); 45 46 switch(v.getId()){ 47 /****************************************************** 48 * 简单对话框加几个setPositiveButton或setNeutralButton或setNegativeButton 49 ******************************************************/ 50 case R.id.bt1: 51 new AlertDialog.Builder(MainActivity.this) 52 .setCancelable(true) 53 .setIcon(R.drawable.ic_launcher) 54 .setTitle("Hi") 55 .setMessage(getText(R.string.hello_world)) 56 .setPositiveButton("Start", new DialogInterface.OnClickListener(){ 57 @Override 58 public void onClick(DialogInterface dialog, 59 int which) { 60 Toast.makeText(MainActivity.this, "Positive", Toast.LENGTH_SHORT).show(); 61 } 62 }) 63 .setNeutralButton("Pause", new DialogInterface.OnClickListener(){ 64 @Override 65 public void onClick(DialogInterface dialog, 66 int which) { 67 Toast.makeText(MainActivity.this, "Neutral", Toast.LENGTH_SHORT).show(); 68 } 69 }) 70 .setNegativeButton("Stop", new DialogInterface.OnClickListener(){ 71 @Override 72 public void onClick(DialogInterface dialog, 73 int which) { 74 Toast.makeText(MainActivity.this, "Negative", Toast.LENGTH_SHORT).show(); 75 } 76 }) 77 .show(); 78 break; 79 /****************************************************** 80 * 点击选择列表对话框setItem() 81 ******************************************************/ 82 case R.id.bt2: 83 new AlertDialog.Builder(MainActivity.this) 84 .setIcon(R.drawable.ic_launcher) 85 .setTitle("点选列表Title") 86 //.setMessage("bt2~MSG")setItems后就不能有msg 87 .setItems(R.array.dialog_item, new DialogInterface.OnClickListener() { 88 @Override 89 public void onClick(DialogInterface dialog, int which) { 90 //item select后的操作 91 new AlertDialog.Builder(MainActivity.this) 92 .setIcon(R.drawable.ic_launcher) 93 .setTitle("选择后") 94 .setMessage("selected MSG:" + itemArray[which]) 95 .setNeutralButton("Posotive", new DialogInterface.OnClickListener() { 96 @Override 97 public void onClick(DialogInterface dialog, int which) { 98 Toast.makeText(MainActivity.this, "Neutral", Toast.LENGTH_SHORT).show(); 99 } 100 }) 101 .show(); 102 } 103 }) 104 .show(); 105 break; 106 /****************************************************** 107 * 单选列表对话框setSingleChoiceItem() 108 ******************************************************/ 109 case R.id.bt3: 110 new AlertDialog.Builder(MainActivity.this) 111 .setIcon(R.drawable.ic_launcher) 112 .setTitle("单选列表Title") 113 //setSingleChoiceItems第二个参数设置显示的默认选中,选项索引从0开始,-1不选中任一项 114 .setSingleChoiceItems(R.array.dialog_item, -1, new DialogInterface.OnClickListener() { 115 @Override 116 public void onClick(DialogInterface dialog, int which) { 117 Toast.makeText(MainActivity.this, "u've selected " + itemArray[which], Toast.LENGTH_LONG).show(); 118 //点击后对话框消失 119 dialog.cancel(); 120 } 121 }).create() 122 .show(); 123 break; 124 /****************************************************** 125 * 复选列表对话框setMultiChoiceItems() 126 ******************************************************/ 127 case R.id.bt4: 128 count = 0; 129 new AlertDialog.Builder(MainActivity.this) 130 .setIcon(R.drawable.ic_launcher) 131 .setTitle("复选列表Title") 132 .setMultiChoiceItems(R.array.dialog_item, null, new DialogInterface.OnMultiChoiceClickListener(){ 133 @Override 134 public void onClick(DialogInterface dialog, int which, boolean isChecked) { 135 String[] array = MainActivity.this.getResources().getStringArray(R.array.dialog_item); 136 String str; 137 if(isChecked) { 138 count++; 139 }else { 140 count--; 141 } 142 Toast.makeText(MainActivity.this, (isChecked ?" 选中了":" 取消了") + itemArray[which], Toast.LENGTH_LONG).show(); 143 } 144 }) 145 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 146 @Override 147 public void onClick(DialogInterface dialog, int which) { 148 Toast.makeText(MainActivity.this, "选择了" + count + "项 ", Toast.LENGTH_LONG).show(); 149 } 150 }) 151 .show(); 152 break; 153 /****************************************************** 154 * 对话框setView() LayoutInflater得到view 155 ******************************************************/ 156 case R.id.bt5: 157 LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 158 View view = inflater.inflate(R.layout.layout_login,null); 159 160 new AlertDialog.Builder(MainActivity.this) 161 .setIcon(R.drawable.ic_launcher) 162 .setTitle("对话框Title") 163 .setView(view) 164 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 165 @Override 166 public void onClick(DialogInterface dialog, int which) { 167 Toast.makeText(MainActivity.this, "登录中...", Toast.LENGTH_LONG).show(); 168 } 169 }) 170 .show(); 171 break; 172 } 173 } 174 }; 175 176 @Override 177 public boolean onCreateOptionsMenu(Menu menu) { 178 // Inflate the menu; this adds items to the action bar if it is present. 179 getMenuInflater().inflate(R.menu.activity_main, menu); 180 return true; 181 } 182 183 } 184 //http://blog.csdn.net/hellogv/article/details/5955959 185 //http://android.blog.51cto.com/268543/333769 186 //http://blog.csdn.net/kuangc2008/article/details/6358915