Android Dialog详解及实例代码
AndroidDialog详解及实例代码
概述:
Android开发中最常用的就是Dialog类,除了自定义dialog布局,最多的就是用在弹出对话框、进度条、输入框、单选、复选框。
1、选择对话框:
AlertDialog.Builderdialog=newAlertDialog.Builder(this); dialog.setTitle("选择对话框"); dialog.setMessage("请选择确认或取消"); dialog.setCancelable(false);//设置按下返回键不能消失 dialog.setPositiveButton("确认",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ Toast.makeText(MainActivity.this,"您选择了确认",Toast.LENGTH_LONG).show(); } }); dialog.setNegativeButton("取消",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ Toast.makeText(MainActivity.this,"您选择了取消",Toast.LENGTH_LONG).show(); } }); dialog.show();//显示弹出窗口
2、进度条
ProgressDialogprogressDialog=newProgressDialog(this); progressDialog.setTitle("显示进度条"); progressDialog.setMessage("加载中..."); progressDialog.setCancelable(true); progressDialog.show();
3、弹出输入框
AlertDialog.Builderbuilder=newAlertDialog.Builder(this); builder.setTitle("弹出输入框"); builder.setMessage("输入内容"); finalEditTexteditText=newEditText(this); builder.setView(editText); builder.setPositiveButton("确定",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ Toast.makeText(MainActivity.this,"您输入了"+editText.getText().toString(),Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); builder.show();
4、弹出单选框
finalAlertDialog.Builderbuilder=newAlertDialog.Builder(this); builder.setTitle("弹出单选框"); finalString[]choice=newString[]{"Java","C++","PHP"}; builder.setSingleChoiceItems(choice,0, newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ Toast.makeText(MainActivity.this,"您选择了"+choice[which],Toast.LENGTH_LONG).show(); dialog.dismiss();//让窗口消失 } }); builder.create().show();
5、弹出复选框
finalAlertDialog.Builderbuilder=newAlertDialog.Builder(this); builder.setTitle("弹出多选框"); finalString[]choice=newString[]{"Android","IOS","WindosPhone"};//选择项 finalboolean[]isCheck=newboolean[choice.length];//是否被选中 builder.setMultiChoiceItems(choice,null, newDialogInterface.OnMultiChoiceClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich,booleanisChecked){ if(isChecked==true) isCheck[which]=true; elseif(isChecked==false) isCheck[which]=false; } }); builder.setNegativeButton("取消",null); builder.setPositiveButton("确定",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ StringisSelected="您选择了"; for(inti=0;i
6、自定义布局的Dialog
AlertDialog.Builderbuilder=newAlertDialog.Builder(this); finalAlertDialogdialog=builder.create(); Viewview=View.inflate(this,R.layout.personal_modifyhead_dialog,null); dialog.setView(view,0,0,0,0);//设置边距为0,保证在2.x的版本上运行没问题 dialog.show(); AutoRelativeLayoutrl_photograph=(AutoRelativeLayout)view.findViewById(R.id.rl_photograph); rl_photograph.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ presenter.modiftHeadFromPhotograph(); dialog.dismiss(); } }); AutoRelativeLayoutrl_album=(AutoRelativeLayout)view.findViewById(R.id.rl_album); rl_album.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ presenter.modifyHeadFromAlbum(); dialog.dismiss(); } });上面就是日常开发中常用的一些,在此记录,以便日后查询。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!