Android Dialog对话框实例代码讲解
Dialog的基本方法
//创建Dialog AlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this); //设置标题图标 builder.setIcon(R.drawable.ic_launcher); //设置标题 builder.setTitle("这是一个对话框"); //设置信息 builder.setMessage("是否要跳转?"); //确定按钮 setPositiveButton(CharSequencetext,DialogInterface.OnClickListenerlistener) //取消按钮 setNegativeButton(CharSequencetext,DialogInterface.OnClickListenerlistener) //忽略 setNeutralButton(CharSequencetext,DialogInterface.OnClickListenerlistener) //显示对话框 show();
系统样式
1.下拉列表
AlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("选择一个城市"); //下拉列表的数据源 finalString[]cities={"北京","上海","广州","深圳","杭州"}; builder.setItems(cities,newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ Toast.makeText(MainActivity.this,"你选择的城市为:"+cities[which],Toast.LENGTH_SHORT).show(); } }); builder.show();
2.单选框列表
AlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("请选择性别"); finalString[]sex={"男","女"}; //第二个参数指定默认哪个单选框被勾中 builder.setSingleChoiceItems(sex,1,newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ Toast.makeText(MainActivity.this,"性别为:"+sex[which],Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ } }); builder.setNegativeButton("取消",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ } }); builder.show();
3.多选框列表
Stringstr; AlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("选择你想看的电视剧"); finalString[]hobbies={"廷禧攻略","扶摇","香蜜沉沉烬如霜","如懿传"}; //第二个参数代表哪几个选项被选择,需要传递一个boolean[]数组进去,其长度要和第一个参数的长度相同,如果null表示都不选 builder.setMultiChoiceItems(hobbies,null,newDialogInterface.OnMultiChoiceClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich,booleanisChecked){ if(isChecked){ str.append(hobbies[which]+","); } Toast.makeText(MainActivity.this,"选择的是:"+str.toString(),Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ } }); builder.setNegativeButton("取消",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ } }); builder.show();
4.等待对话框
ProgressDialogwaitingDialog=newProgressDialog(MainActivity.this); waitingDialog.setTitle("等待加载中,请稍后..."); waitingDialog.setMessage("等待中..."); waitingDialog.setIndeterminate(true);//采用不确定式的进度条 waitingDialog.setCancelable(false);//点击外部不取消对话框 waitingDialog.show();
5.进度条对话框
intMAXPD=100; ProgressDialogprogressDialog=newProgressDialog(MainActivity.this); progressDialog.setProgress(0);//设置默认值 progressDialog.setTitle("正在下载"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//进度条样式 progressDialog.setMax(MAXPD);//设置最大值 progressDialog.show();
自定义Dialog
1.继承Dialog
publicclassCustomDialogextendsDialog{ //标题 protectedTextViewhintTv; //左边按钮 protectedButtondoubleLeftBtn; //右边按钮 protectedButtondoubleRightBtn; //输入框 publicEditTexteditText; publicCustomDialog(Contextcontext){ super(context,R.style.CustomDialogStyle); } @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); this.setCancelable(false);//是否可以撤销 setContentView(R.layout.dialog_custom); hintTv=findViewById(R.id.tv_common_dialog_hint); doubleLeftBtn=findViewById(R.id.btn_common_dialog_double_left); doubleRightBtn=findViewById(R.id.btn_common_dialog_double_right); editText=findViewById(R.id.et_common_dialog); } //置右键文字和点击事件 publicvoidsetRightButton(StringrightStr,View.OnClickListenerclickListener){ doubleRightBtn.setOnClickListener(clickListener); doubleRightBtn.setText(rightStr); } //设置左键文字和点击事件 publicvoidsetLeftButton(StringleftStr,View.OnClickListenerclickListener){ doubleLeftBtn.setOnClickListener(clickListener); doubleLeftBtn.setText(leftStr); } //设置提示内容 publicvoidsetHintText(Stringstr){ hintTv.setText(str); hintTv.setVisibility(View.VISIBLE); } //给两个按钮设置文字 publicvoidsetBtnText(StringleftStr,StringrightStr){ doubleLeftBtn.setText(leftStr); doubleRightBtn.setText(rightStr); } }
2.自定义Style
@null @color/transparent true true false @null true @color/white @color/white
3.自定义布局
4.ipad隐藏底部虚拟按键
//弹出dialog时隐藏底部虚拟按键 dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(newView.OnSystemUiVisibilityChangeListener(){ @Override publicvoidonSystemUiVisibilityChange(intvisibility){ intuiOptions=View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; if(Build.VERSION.SDK_INT>=19){ uiOptions|=0x00001000; }else{ uiOptions|=View.SYSTEM_UI_FLAG_LOW_PROFILE; } dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions); } });
5.使用自定义Dialog
CustomDialogdialog=newCustomDialog(this); dialog.setHintText("请输入密码"); dialog.setLeftButton("取消",newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ dialog.dismiss(); } }); dialog.setRightButton("确定",newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ } }); dialog.show();
总结
以上所述是小编给大家介绍的AndroidDialog对话框实例代码讲解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!