22,//Dialog用法,7个小例子
<1>//main.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 <TextView 7 android:layout_width="fill_parent" 8 android:layout_height="wrap_content" 9 android:text="@string/hello" /> 10 <Button 11 android:id="@+id/Two_but" 12 android:layout_width="fill_parent" 13 android:layout_height="wrap_content" 14 android:text="两个按钮的对话框" 15 /> 16 <Button 17 android:id="@+id/Three_but" 18 android:layout_width="fill_parent" 19 android:layout_height="wrap_content" 20 android:text="三个按钮的对话框" 21 /> 22 <Button 23 android:id="@+id/putIn_but" 24 android:layout_width="fill_parent" 25 android:layout_height="wrap_content" 26 android:text="可以输入的对话框" 27 /> 28 <Button 29 android:id="@+id/scroll_but" 30 android:layout_width="fill_parent" 31 android:layout_height="wrap_content" 32 android:text="滚动对话框" 33 /> 34 <Button 35 android:id="@+id/radio_but" 36 android:layout_width="fill_parent" 37 android:layout_height="wrap_content" 38 android:text="单选对话框" 39 /> 40 <Button 41 android:id="@+id/check_but" 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:text="多选对话框" 45 /> 46 <Button 47 android:id="@+id/list_but" 48 android:layout_width="fill_parent" 49 android:layout_height="wrap_content" 50 android:text="列表对话框" 51 /> 52 </LinearLayout>
//alert_dialog_text_entry.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <TextView 7 android:id="@+id/username" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:layout_marginLeft="20dip" 11 android:layout_marginRight="20dip" 12 android:text="@string/username" 13 /> 14 <EditText 15 android:id="@+id/usernameEdit" 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:layout_marginLeft="20dip" 19 android:layout_marginRight="20dip" 20 android:capitalize="none" 21 /> 22 <TextView 23 android:id="@+id/passward" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_marginLeft="20dip" 27 android:layout_marginRight="20dip" 28 android:text="@string/passward" 29 /> 30 <EditText 31 android:id="@+id/passwardEdit" 32 android:layout_width="fill_parent" 33 android:layout_height="wrap_content" 34 android:layout_marginLeft="20dip" 35 android:layout_marginRight="20dip" 36 android:password="true" 37 android:capitalize="none" 38 /> 39 </LinearLayout>
<2>//Java文件
1 public class DialogActivity extends Activity { 2 private Button mtwoButton = null; 3 private Button mthreeButton = null; 4 private Button minputButton = null; 5 private Button mscrollButton = null; 6 private Button radioButton = null; 7 private Button checkButton = null; 8 private Button listButton = null; 9 private static final int DIALOG1 = 0; 10 private static final int DIALOG2 = 1; 11 private static final int DIALOG3 = 2; 12 private static final int DIALOG4 = 3; 13 private static final int DIALOG5 = 4; 14 private static final int DIALOG6 = 5; 15 private static final int DIALOG7 = 6; 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.main); 20 mtwoButton = (Button)findViewById(R.id.Two_but); 21 mthreeButton = (Button)findViewById(R.id.Three_but); 22 minputButton = (Button)findViewById(R.id.putIn_but); 23 mscrollButton = (Button)findViewById(R.id.scroll_but); 24 radioButton = (Button)findViewById(R.id.radio_but); 25 checkButton = (Button)findViewById(R.id.check_but); 26 listButton = (Button)findViewById(R.id.list_but); 27 28 mtwoButton.setOnClickListener(new OnClickListener(){ 29 30 @Override 31 public void onClick(View v) { 32 showDialog(DIALOG1); 33 } 34 35 }); 36 mthreeButton.setOnClickListener(new OnClickListener(){ 37 38 @Override 39 public void onClick(View v) { 40 showDialog(DIALOG2); 41 } 42 43 }); 44 minputButton.setOnClickListener(new OnClickListener(){ 45 46 @Override 47 public void onClick(View v) { 48 showDialog(DIALOG3); 49 } 50 51 }); 52 mscrollButton.setOnClickListener(new OnClickListener(){ 53 54 @Override 55 public void onClick(View v) { 56 showDialog(DIALOG4); 57 } 58 59 }); 60 radioButton.setOnClickListener(new OnClickListener() { 61 62 @Override 63 public void onClick(View v) { 64 showDialog(DIALOG5); 65 } 66 }); 67 checkButton.setOnClickListener(new OnClickListener() { 68 69 @Override 70 public void onClick(View v) { 71 showDialog(DIALOG6); 72 } 73 }); 74 listButton.setOnClickListener(new OnClickListener() { 75 76 @Override 77 public void onClick(View v) { 78 showDialog(DIALOG7); 79 } 80 }); 81 } 82 protected Dialog onCreateDialog(int id){ 83 switch(id){ 84 case DIALOG1: 85 return buildDialog1(DialogActivity.this); 86 case DIALOG2: 87 return buildDialog2(DialogActivity.this); 88 case DIALOG3: 89 return buildDialog3(DialogActivity.this); 90 case DIALOG4: 91 return buildDialog4(DialogActivity.this); 92 case DIALOG5: 93 return buildDialog5(DialogActivity.this); 94 case DIALOG6: 95 return buildDialog6(DialogActivity.this); 96 case DIALOG7: 97 return buildDialog7(DialogActivity.this); 98 } 99 return null; 100 } 101 private Dialog buildDialog1(Context context){ 102 AlertDialog.Builder builder = new AlertDialog.Builder(context); 103 builder.setIcon(R.drawable.alert_dialog_icon); 104 builder.setTitle(R.string.alert_dialog_two_button_title); 105 builder.setPositiveButton(R.string.alert_dialog_ok, 106 new DialogInterface.OnClickListener() { 107 @Override 108 public void onClick(DialogInterface dialog, int which) { 109 setTitle("单击对话框上的确定按钮"); 110 111 } 112 }); 113 builder.setNegativeButton(R.string.alert_dialog_cancel, 114 new DialogInterface.OnClickListener() { 115 @Override 116 public void onClick(DialogInterface dialog, int which) { 117 setTitle("单击了对话框上的取消按钮"); 118 } 119 }); 120 121 return builder.create(); 122 } 123 private Dialog buildDialog2(Context context){ 124 AlertDialog.Builder builder = new AlertDialog.Builder(context); 125 builder.setIcon(R.drawable.alert_dialog_icon); 126 builder.setTitle(R.string.alert_dialog_two_button_msg); 127 builder.setMessage(R.string.alert_dialog_two_button2_msg); 128 builder.setPositiveButton(R.string.alert_dialog_ok, 129 new DialogInterface.OnClickListener() { 130 131 @Override 132 public void onClick(DialogInterface dialog, int which) { 133 setTitle("单击对话框上的确定按钮"); 134 135 } 136 }); 137 builder.setNeutralButton(R.string.alert_dialog_something, 138 new DialogInterface.OnClickListener() { 139 140 @Override 141 public void onClick(DialogInterface dialog, int which) { 142 setTitle("点击了对话框中的详情按钮"); 143 } 144 }); 145 builder.setNegativeButton(R.string.alert_dialog_cancel, 146 new DialogInterface.OnClickListener() { 147 148 @Override 149 public void onClick(DialogInterface dialog, int which) { 150 setTitle("单击了对话框上的取消按钮"); 151 } 152 }); 153 154 return builder.create(); 155 } 156 private Dialog buildDialog3(Context context){ 157 LayoutInflater inflater = LayoutInflater.from(this); 158 //引用其他xml文件中的内容 159 final View textEntryView = inflater.inflate(R.layout.alert_dialog_text_entry, null); 160 AlertDialog.Builder builder = new AlertDialog.Builder(context); 161 builder.setIcon(R.drawable.alert_dialog_icon); 162 builder.setTitle(R.string.alert_dialog_entry_title); 163 builder.setView(textEntryView);//把View放到Dialog中,关键所在 164 builder.setPositiveButton(R.string.alert_dialog_ok, 165 new DialogInterface.OnClickListener() { 166 167 @Override 168 public void onClick(DialogInterface dialog, int which) { 169 setTitle("单击对话框上的确定按钮"); 170 171 } 172 }); 173 builder.setNegativeButton(R.string.alert_dialog_cancel, 174 new DialogInterface.OnClickListener() { 175 176 @Override 177 public void onClick(DialogInterface dialog, int which) { 178 setTitle("单击了对话框上的取消按钮"); 179 } 180 }); 181 182 return builder.create(); 183 } 184 private Dialog buildDialog4(Context context){ 185 ProgressDialog dialog = new ProgressDialog(context); 186 dialog.setTitle("正在下载歌曲"); 187 dialog.setMessage("请稍候…………"); 188 return dialog; 189 } 190 private Dialog buildDialog5(Context context){ 191 AlertDialog.Builder builder = new AlertDialog.Builder(context); 192 builder.setIcon(R.drawable.alert_dialog_icon); 193 builder.setTitle(R.string.alert_dialog_radio_title); 194 builder.setSingleChoiceItems(new String[] { "Item1", "Item2" }, 0, 195 new DialogInterface.OnClickListener() { 196 public void onClick(DialogInterface dialog, int which) { 197 dialog.dismiss(); 198 } 199 }); 200 builder.setNegativeButton("取消", null).show(); 201 return builder.create(); 202 } 203 private Dialog buildDialog6(Context context){ 204 AlertDialog.Builder builder = new AlertDialog.Builder(context); 205 builder.setIcon(R.drawable.alert_dialog_icon); 206 builder.setTitle(R.string.alert_dialog_check_title); 207 builder.setMultiChoiceItems(new String[] { "Item1", "Item2" }, null,null); 208 builder.setNegativeButton("确定",null).show(); 209 builder.setPositiveButton("取消",null).show(); 210 return builder.create(); 211 } 212 private Dialog buildDialog7(Context context){ 213 AlertDialog.Builder builder = new AlertDialog.Builder(context); 214 builder.setIcon(R.drawable.alert_dialog_icon); 215 builder.setTitle(R.string.alert_dialog_item_title); 216 builder.setItems(new String[] { "Item1", "Item2" }, null); 217 builder.setNegativeButton("确定",null).show(); 218 builder.setPositiveButton("取消",null).show(); 219 220 return builder.create(); 221 } 222 protected void onPrepareDialog(int id,Dialog diallog){ 223 if(id == DIALOG1){ 224 setTitle("测试"); 225 } 226 } 227 }
忘记截图了,呵呵