Android学习之Dialog
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框;
实例如下:
1.效果图:
2.XML代码:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 12 <Button 13 android:id="@+id/button1" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_alignParentLeft="true" 17 android:layout_alignParentTop="true" 18 android:text="信息提示框" /> 19 20 <Button 21 android:id="@+id/button2" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_alignLeft="@+id/button1" 25 android:layout_below="@+id/button1" 26 android:text="弹出单选按钮框" /> 27 28 <Button 29 android:id="@+id/button3" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_alignRight="@+id/button2" 33 android:layout_below="@+id/button2" 34 android:text="弹出多选按钮框" /> 35 36 <Button 37 android:id="@+id/button4" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:layout_alignLeft="@+id/button3" 41 android:layout_below="@+id/button3" 42 android:text="弹出一组列表框" /> 43 44 <Button 45 android:id="@+id/button5" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 android:layout_alignLeft="@+id/button4" 49 android:layout_below="@+id/button4" 50 android:text="弹出进度条框" /> 51 52 <Button 53 android:id="@+id/button6" 54 android:layout_width="wrap_content" 55 android:layout_height="wrap_content" 56 android:layout_alignLeft="@+id/button5" 57 android:layout_below="@+id/button5" 58 android:text="自定义布局框" /> 59 60 </RelativeLayout>
3.java代码:
1 package com.example.alertdilog; 2 3 import android.app.Activity; 4 import android.app.Dialog; 5 import android.app.AlertDialog; 6 import android.app.ProgressDialog; 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.EditText; 14 import android.widget.Toast; 15 16 import android.os.Bundle; 17 18 public class MainActivity extends Activity implements OnClickListener { 19 20 private Button Buttton1; 21 private Button Buttton2; 22 private Button Buttton3; 23 private Button Buttton4; 24 private Button Buttton5; 25 private Button Buttton6; 26 27 28 private EditText mUserNamEditText; 29 private EditText mPwdEditText; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 36 initView(); 37 38 } 39 40 private void initView() { 41 Buttton1 = (Button) this.findViewById(R.id.button1); 42 Buttton2 = (Button) this.findViewById(R.id.button2); 43 Buttton3 = (Button) this.findViewById(R.id.button3); 44 Buttton4 = (Button) this.findViewById(R.id.button4); 45 Buttton5 = (Button) this.findViewById(R.id.button5); 46 Buttton6 = (Button) this.findViewById(R.id.button6); 47 48 49 Buttton1.setOnClickListener(this); 50 Buttton2.setOnClickListener(this); 51 Buttton3.setOnClickListener(this); 52 Buttton4.setOnClickListener(this); 53 Buttton5.setOnClickListener(this); 54 Buttton6.setOnClickListener(this); 55 } 56 57 58 @Override 59 public boolean onCreateOptionsMenu(Menu menu) { 60 // Inflate the menu; this adds items to the action bar if it is present. 61 getMenuInflater().inflate(R.menu.main, menu); 62 return true; 63 } 64 65 @Override 66 public void onClick(View view) { 67 switch (view.getId()) { 68 case R.id.button1: // 信息提示框 69 AlertDialog(); 70 break; 71 case R.id.button2: 72 AlertRadioDialog(); // 弹出单选按钮 73 break; 74 case R.id.button3: 75 AlertCheckBoxDialog(); 76 break; 77 case R.id.button4: 78 AlertListDialog(); 79 break; 80 case R.id.button5: 81 AlertProgressDialog(); 82 break; 83 case R.id.button6: 84 AlertziDialog(); 85 break; 86 87 default: 88 break; 89 } 90 } 91 92 private void AlertziDialog() { 93 // 动态加载布局生成View对象 94 LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this); 95 View view = layoutInflater.inflate(R.layout.login_alert, null); 96 mUserNamEditText = (EditText) view.findViewById(R.id.edit_username); 97 mPwdEditText = (EditText) view.findViewById(R.id.edit_password); 98 99 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 100 builder.setTitle("登陆"); 101 builder.setIcon(R.drawable.ic_launcher).create(); 102 builder.setView(view); 103 builder.setPositiveButton("确定", null); 104 builder.setNegativeButton("取消", null); 105 builder.show(); 106 } 107 108 private void AlertListDialog() { 109 // 指定下拉列表的显示数据 110 final String[] cities = { "广州", "上海", "北京", "香港", "澳门", "深圳", "天津", 111 "武汉", "兰州", "长沙", "重庆", "西安", "成都", "安徽" }; 112 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 113 builder.setIcon(R.drawable.ic_launcher); 114 builder.setTitle("选择一个城市"); 115 // 设置一个下拉的列表选择项 116 builder.setItems(cities, new DialogInterface.OnClickListener() { 117 @Override 118 public void onClick(DialogInterface dialog, int which) { 119 Toast.makeText(MainActivity.this, "选择的城市为:" + cities[which], 120 Toast.LENGTH_SHORT).show(); 121 } 122 }); 123 builder.show(); 124 } 125 126 private void AlertRadioDialog() { 127 final String[] sex = { "男", "女", "保密" }; 128 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 129 builder.setTitle("请选择性别"); 130 builder.setIcon(android.R.drawable.ic_dialog_info).create(); 131 builder.setSingleChoiceItems(sex, 0, 132 new DialogInterface.OnClickListener() { 133 public void onClick(DialogInterface dialog, int which) { 134 Toast.makeText(MainActivity.this, "选择的是:" + sex[which], 135 Toast.LENGTH_SHORT).show(); 136 dialog.dismiss(); 137 } 138 }); 139 builder.setNegativeButton("取消", null); 140 builder.show(); 141 } 142 143 private void AlertCheckBoxDialog() { 144 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 145 builder.setTitle("请选择爱好"); 146 builder.setIcon(R.drawable.ic_launcher).create(); 147 builder.setMultiChoiceItems(new String[] { "打球", "谈朋友", "看小说", "写代码" }, 148 null, null); 149 builder.setPositiveButton("确定", null); 150 builder.setNegativeButton("取消", null); 151 builder.show(); 152 } 153 154 private void AlertDialog() { 155 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 156 builder.setTitle("信息提示"); 157 builder.setMessage("删除后不能回复,确认要删除吗?"); 158 builder.setIcon(R.drawable.ic_launcher).create(); 159 builder.setPositiveButton("确认", new AlertDialog.OnClickListener() { 160 @Override 161 public void onClick(DialogInterface dialog, int which) { 162 dialog.dismiss(); 163 MainActivity.this.finish(); 164 } 165 }); 166 builder.setNegativeButton("取消", new AlertDialog.OnClickListener() { 167 @Override 168 public void onClick(DialogInterface dialog, int which) { 169 dialog.dismiss(); 170 } 171 }); 172 builder.show(); 173 } 174 175 private void AlertProgressDialog() { 176 ProgressDialog mypDialog = new ProgressDialog(MainActivity.this); 177 // 设置ProgressDialog 标题 178 mypDialog.setTitle("信息提示框"); 179 // 设置ProgressDialog 提示信息 180 mypDialog.setMessage("Loading.........."); 181 // 设置ProgressDialog 标题图标 182 mypDialog.setIcon(R.drawable.ic_launcher); 183 // 设置ProgressDialog 进度条进度 184 mypDialog.setProgress(59); 185 // 设置ProgressDialog 的进度条是否不明确 186 mypDialog.setIndeterminate(true); 187 // 设置ProgressDialog 是否可以按退回按键取消 188 mypDialog.setCancelable(true); 189 // 显示出来来 190 mypDialog.show(); 191 } 192 193 }