各种对话框(DialogListActivity)

DialogListActivity.java
1 import java.util.ArrayList; 2 import java.util.Calendar; 3 import java.util.List; 4 5 import android.app.Activity; 6 import android.app.AlertDialog; 7 import android.app.DatePickerDialog; 8 import android.app.DatePickerDialog.OnDateSetListener; 9 import android.app.ProgressDialog; 10 import android.app.TimePickerDialog; 11 import android.app.TimePickerDialog.OnTimeSetListener; 12 import android.content.DialogInterface; 13 import android.os.Bundle; 14 import android.os.Handler; 15 import android.view.LayoutInflater; 16 import android.view.View; 17 import android.view.View.OnClickListener; 18 import android.widget.Button; 19 import android.widget.DatePicker; 20 import android.widget.EditText; 21 import android.widget.TimePicker; 22 import android.widget.Toast; 23 /* 本Activity仅为测试不同 Dialog 的使用 24 * 重复性代码较多,如果用于开发可以将对话框创建封装一下 25 * */ 26 public class DialogListActivity extends Activity { 27 28 Button b1,b2,b3,b4,b5,b6,b7,b8; 29 ProgressDialog pd; 30 Handler handler; 31 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 // TODO Auto-generated method stub 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.dialoglistlayout); 37 38 b1=(Button) findViewById(R.id.button1); 39 b2=(Button) findViewById(R.id.button2); 40 b3=(Button) findViewById(R.id.button3); 41 b4=(Button) findViewById(R.id.button4); 42 b5=(Button) findViewById(R.id.button5); 43 b6=(Button) findViewById(R.id.button6); 44 b7=(Button) findViewById(R.id.button7); 45 b8=(Button) findViewById(R.id.button8); 46 47 //创建监听器并注册给控件 48 BtClickListener listener = new BtClickListener(); 49 b1.setOnClickListener(listener); 50 b2.setOnClickListener(listener); 51 b3.setOnClickListener(listener); 52 b4.setOnClickListener(listener); 53 b5.setOnClickListener(listener); 54 b6.setOnClickListener(listener); 55 b7.setOnClickListener(listener); 56 b8.setOnClickListener(listener); 57 58 handler = new Handler(); 59 } 60 61 //创建简单对话框 62 public void createCommonDialog(View v){ 63 //创建Builder 64 AlertDialog.Builder builder = new AlertDialog.Builder(this); 65 //设置对话框的标题和图标 66 builder.setIcon(android.R.drawable.ic_delete); 67 builder.setTitle("删除信息"); 68 //设置信息 69 builder.setMessage("请确认是否删除 "+v.getId()); 70 //左边按钮 71 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 72 @Override 73 public void onClick(DialogInterface dialog, int which) { 74 // 75 } 76 }); 77 //右边按钮 78 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 79 @Override 80 public void onClick(DialogInterface dialog, int which) { 81 // 82 } 83 }); 84 builder.show(); 85 } 86 87 //自定义创建 列表模式对话框 88 public void createListDialog(View v){ 89 AlertDialog.Builder builder = new AlertDialog.Builder(this); 90 builder.setTitle("请选择送货时间"); 91 builder.setIcon(android.R.drawable.ic_menu_directions); 92 //设置列表,并注入监听器 93 final String []ss={"星期一","星期二","星期三","星期四","星期五","星期六","星期天"}; 94 builder.setItems(ss, new DialogInterface.OnClickListener(){ 95 @Override 96 public void onClick(DialogInterface dialog, int which) { 97 Toast.makeText(DialogListActivity.this,"您选择的时间是"+ss[which], Toast.LENGTH_SHORT) 98 .show(); 99 }} 100 ); 101 //左边按钮 102 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 103 @Override 104 public void onClick(DialogInterface dialog, int which) { 105 // 106 } 107 }); 108 //右边按钮 109 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 110 @Override 111 public void onClick(DialogInterface dialog, int which) { 112 // 113 } 114 }); 115 builder.show(); 116 } 117 118 //创建复选模式对话框 119 public void createMultipleDialog(View v){ 120 AlertDialog.Builder builder = new AlertDialog.Builder(this); 121 builder.setTitle("请选择曾用手机品牌"); 122 builder.setIcon(android.R.drawable.ic_menu_gallery); 123 //设置列表,并注入监听器 124 final String []ss={"联想","三星","苹果","诺基亚","黑莓","摩托罗拉","其他"}; 125 final List <String>item = new ArrayList<String>(); 126 builder.setMultiChoiceItems(ss, 127 new boolean[]{false,false,false,false,false,false,false}, 128 new DialogInterface.OnMultiChoiceClickListener(){ 129 @Override 130 public void onClick(DialogInterface dialog, int which, 131 boolean isChecked) { 132 if(isChecked){ 133 item.add(ss[which]);//将选中的数据添加入列表 134 }else{ 135 if(item.contains(ss[which]))item.remove(ss[which]); 136 } 137 }} 138 ); 139 //左边按钮 140 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 141 @Override 142 public void onClick(DialogInterface dialog, int which) { 143 Toast.makeText(DialogListActivity.this, "您选择的是"+item.toString(),Toast.LENGTH_LONG) 144 .show(); 145 } 146 }); 147 //右边按钮 148 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 149 @Override 150 public void onClick(DialogInterface dialog, int which) { 151 // 152 } 153 }); 154 builder.show(); 155 } 156 157 //创建单选模式对话框 158 public void createSingleDialog(View v){ 159 AlertDialog.Builder builder = new AlertDialog.Builder(this); 160 builder.setTitle("您的年龄"); 161 builder.setIcon(android.R.drawable.ic_secure); 162 //设置列表,并注入监听器 163 final String []ss={"总角之年","弱冠之年","而立之年","不惑之年","知命之年","耳顺之年"}; 164 builder.setSingleChoiceItems(ss,2, new DialogInterface.OnClickListener(){ 165 @Override 166 public void onClick(DialogInterface dialog, int which) { 167 Toast.makeText(DialogListActivity.this, "您选择的是 "+ss[which],Toast.LENGTH_LONG) 168 .show(); 169 }} 170 ); 171 //左边按钮 172 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 173 @Override 174 public void onClick(DialogInterface dialog, int which) { 175 } 176 }); 177 //右边按钮 178 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 179 @Override 180 public void onClick(DialogInterface dialog, int which) { 181 // 182 } 183 }); 184 builder.show(); 185 } 186 187 //创建进度条对话框 188 public void createProgressDialog(){ 189 pd = new ProgressDialog(this); 190 pd.setIcon(android.R.drawable.ic_menu_upload); 191 pd.setTitle("下载"); 192 pd.setMessage("文件下载中,请稍后……"); 193 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 194 pd.setMax(100); 195 pd.show(); 196 //子线程修改进度 197 //new Thread(new MyThread()).start(); 198 //ProgressDialog.show(this, "文件下载", "文件正在下载,请稍后……"); 199 200 } 201 202 //日期选择对话框 203 public void createDatePickerDialog(){ 204 Calendar c=Calendar.getInstance(); 205 DatePickerDialog dpd=new DatePickerDialog(this, 206 new OnDateSetListener(){ 207 @Override 208 public void onDateSet(DatePicker view, int year, 209 int monthOfYear, int dayOfMonth) { 210 setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth); 211 }}, 212 c.get(Calendar.YEAR), 213 c.get(Calendar.MONTH), 214 c.get(Calendar.DAY_OF_MONTH)); 215 dpd.setIcon(android.R.drawable.ic_dialog_alert); 216 dpd.setTitle("请选择日期"); 217 dpd.show(); 218 } 219 220 //时间选择器 221 public void createTimePickerDialog(){ 222 TimePickerDialog tpd =new TimePickerDialog( 223 this, 224 new OnTimeSetListener(){ 225 @Override 226 public void onTimeSet(TimePicker view, int hourOfDay, 227 int minute) { 228 setTitle("选定时间:"+hourOfDay+":"+minute); 229 }}, 230 18,16,true 231 ); 232 tpd.setTitle("请选择时间"); 233 tpd.show(); 234 } 235 236 //创建自定义对话框 237 public void createLoginDialog(){ 238 AlertDialog.Builder builder =new AlertDialog.Builder(this); 239 builder.setTitle("用户登录"); 240 builder.setIcon(android.R.drawable.ic_menu_myplaces); 241 // 242 LayoutInflater inflater = LayoutInflater.from(this); 243 View loginView = inflater.inflate(R.layout.logindialoglayout, null); 244 final EditText name; 245 final EditText pwd; 246 name=(EditText) loginView.findViewById(R.id.login_name); 247 pwd=(EditText) loginView.findViewById(R.id.login_password); 248 builder.setView(loginView); 249 builder.setPositiveButton("登录",new DialogInterface.OnClickListener(){ 250 @Override 251 public void onClick(DialogInterface dialog, int which) { 252 String n=name.getText().toString(); 253 String p=pwd.getText().toString(); 254 Toast.makeText(DialogListActivity.this, "登录信息:"+n+" "+p, Toast.LENGTH_LONG) 255 .show(); 256 }} 257 ); 258 builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { 259 @Override 260 public void onClick(DialogInterface dialog, int which) { 261 } 262 }); 263 builder.show(); 264 } 265 266 class MyThread implements Runnable{ 267 @Override 268 public void run() { 269 int pro = 0; 270 while(pd.getProgress() < pd.getMax()){ 271 try { 272 Thread.sleep(100); 273 handler.post(new Runnable(){ 274 @Override 275 public void run() { 276 pd.setProgress(pd.getProgress()+2); 277 }} 278 ); 279 } catch (InterruptedException e) { 280 // TODO Auto-generated catch block 281 e.printStackTrace(); 282 } 283 } 284 } 285 } 286 287 class BtClickListener implements OnClickListener{ 288 @Override 289 public void onClick(View v) { 290 if(v.getId() == R.id.button1){ 291 //创建简单对话框 292 createCommonDialog(v); 293 }else if(v.getId() == R.id.button2){ 294 //创建简单对话框 295 createListDialog(v); 296 }else if(v.getId() == R.id.button3){ 297 //创建简单对话框 298 createMultipleDialog(v); 299 }else if(v.getId() == R.id.button4){ 300 //创建简单对话框 301 createSingleDialog(v); 302 }else if(v.getId() == R.id.button5){ 303 //创建进度条对话框 304 createProgressDialog(); 305 }else if(v.getId() == R.id.button6){ 306 //创建日期选择对话框 307 createDatePickerDialog(); 308 }else if(v.getId() == R.id.button7){ 309 //创建时间选择对话框 310 createTimePickerDialog(); 311 }else if(v.getId() == R.id.button8){ 312 //创建自定义对话框 313 createLoginDialog(); 314 } 315 } 316 } 317 }
DialogMenuActivity.java
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.List; 4 import java.util.Map; 5 6 import android.app.Activity; 7 import android.app.AlertDialog; 8 import android.os.Bundle; 9 import android.view.LayoutInflater; 10 import android.view.Menu; 11 import android.view.View; 12 import android.widget.AdapterView; 13 import android.widget.AdapterView.OnItemClickListener; 14 import android.widget.GridView; 15 import android.widget.SimpleAdapter; 16 import android.widget.Toast; 17 18 public class DialogMenuActivity extends Activity { 19 20 GridView gv; //对话框视图需要的控件 21 AlertDialog dialog; //对话框 22 List<Map<String,Object>> data; //gv需要的数据 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.main); 28 29 //初始化对话框,但不显示对话框 30 initDialog(); 31 } 32 33 //初始化 对话框 34 public void initDialog(){ 35 dialog = new AlertDialog.Builder(this).create(); 36 View dialogView = LayoutInflater.from(this).inflate(R.layout.gridviewdialog, null); 37 //设置对话框界面 38 dialog.setView(dialogView); 39 //获取对话框界面上的GridView控件 40 gv = (GridView) dialogView.findViewById(R.id.dialog_gridview); 41 gv.setAdapter(createMenuAdapter()); 42 gv.setOnItemClickListener(new OnItemClickListener(){ 43 @Override 44 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 45 Toast.makeText(DialogMenuActivity.this, "您选择的是第" + arg2 + "个菜单", Toast.LENGTH_LONG) 46 .show(); 47 }} 48 ); 49 //注意此处不要调用show 50 } 51 52 //创建 GridView 的数据适配器 53 public SimpleAdapter createMenuAdapter(){ 54 55 data = new ArrayList<Map<String,Object>>(); 56 57 int [] icons = { 58 android.R.drawable.ic_menu_edit, 59 android.R.drawable.ic_menu_delete, 60 android.R.drawable.ic_menu_add, 61 android.R.drawable.ic_menu_save, 62 android.R.drawable.ic_menu_search, 63 android.R.drawable.ic_menu_slideshow, 64 android.R.drawable.ic_menu_share, 65 android.R.drawable.ic_menu_manage 66 }; 67 68 String []titles = {"编辑","删除","添加","保存", 69 "查找","设为背景","分享","设置"}; 70 71 for(int i=0; i<icons.length; i++){ 72 Map<String,Object> item = new HashMap<String,Object>(); 73 item.put("icon", icons[i]); 74 item.put("title", titles[i]); 75 76 data.add(item); 77 } 78 79 SimpleAdapter sa = new SimpleAdapter(this, 80 data, 81 R.layout.gridviewdialog_item_1, 82 new String[]{"icon", "title"}, 83 new int[]{R.id.gridviewdialog_img, R.id.gridviewdialog_tv} 84 ); 85 return sa; 86 } 87 88 @Override 89 public boolean onMenuOpened(int featureId, Menu menu) { 90 dialog.show(); 91 return false; 92 } 93 94 @Override 95 public boolean onCreateOptionsMenu(Menu menu) { 96 menu.add("test"); 97 return super.onCreateOptionsMenu(menu); 98 } 99 }
main.xml
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 7 <TextView 8 android:id="@+id/tv_menu" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="" 12 /> 13 14 </LinearLayout>
dialoglistlayout.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 > 6 <LinearLayout 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 android:orientation="vertical" > 10 <Button 11 android:id="@+id/button1" 12 android:layout_width="fill_parent" 13 android:layout_height="wrap_content" 14 android:text="简单信息对话框" /> 15 <Button 16 android:id="@+id/button2" 17 android:layout_width="fill_parent" 18 android:layout_height="wrap_content" 19 android:text="列表模式对话框" /> 20 <Button 21 android:id="@+id/button3" 22 android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:text="复选模式对话框" /> 25 <Button 26 android:id="@+id/button4" 27 android:layout_width="fill_parent" 28 android:layout_height="wrap_content" 29 android:text="单选模式对话框" /> 30 <Button 31 android:id="@+id/button5" 32 android:layout_width="fill_parent" 33 android:layout_height="wrap_content" 34 android:text="进度条对话框" /> 35 <Button 36 android:id="@+id/button6" 37 android:layout_width="fill_parent" 38 android:layout_height="wrap_content" 39 android:text="日期选择对话框" /> 40 <Button 41 android:id="@+id/button7" 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:text="时间选择对话框" /> 45 <Button 46 android:id="@+id/button8" 47 android:layout_width="fill_parent" 48 android:layout_height="wrap_content" 49 android:text="自定义对话框" /> 50 </LinearLayout> 51 </ScrollView>
logindialoglayout.xml
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:padding="10dp" 6 android:layout_margin="10dp" 7 android:orientation="vertical" > 8 <LinearLayout 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:orientation="horizontal" 12 > 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="账号:" 17 /> 18 <EditText 19 android:id="@+id/login_name" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:singleLine="true" 23 android:hint="输入账户" 24 /> 25 </LinearLayout> 26 <LinearLayout 27 android:layout_width="fill_parent" 28 android:layout_height="wrap_content" 29 android:orientation="horizontal" 30 > 31 <TextView 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="密码:" 35 /> 36 <EditText 37 android:id="@+id/login_password" 38 android:layout_width="match_parent" 39 android:layout_height="wrap_content" 40 android:singleLine="true" 41 android:inputType="textPassword" 42 android:hint="输入密码" 43 /> 44 </LinearLayout> 45 </LinearLayout>
gridviewdialog.xml
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 7 <GridView 8 android:id="@+id/dialog_gridview" 9 android:layout_width="fill_parent" 10 android:layout_height="fill_parent" 11 android:numColumns="4" 12 android:stretchMode="columnWidth" 13 android:horizontalSpacing="10dp" 14 android:verticalSpacing="10dp" 15 android:gravity="center_horizontal" 16 > 17 18 </GridView> 19 20 </LinearLayout>
gridviewdialog_item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <ImageView 7 android:id="@+id/gridviewdialog_img" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:layout_centerHorizontal="true" 11 /> 12 13 <TextView 14 android:id="@+id/gridviewdialog_tv" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:layout_centerHorizontal="true" 18 android:layout_below="@id/gridviewdialog_img" 19 /> 20 </RelativeLayout>

浙公网安备 33010602011771号