Dialog的基本方法
创新互联公司-专业网站定制、快速模板网站建设、高性价比天柱网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式天柱网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖天柱地区。费用合理售后完善,十多年实体公司更值得信赖。
//创建Dialog AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //设置标题图标 builder.setIcon(R.drawable.ic_launcher); //设置标题 builder.setTitle("这是一个对话框"); //设置信息 builder.setMessage("是否要跳转?"); //确定按钮 setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) //取消按钮 setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener) //忽略 setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener) //显示对话框 show();
系统样式
1.下拉列表
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("选择一个城市"); //下拉列表的数据源 final String[] cities = {"北京", "上海", "广州", "深圳", "杭州"}; builder.setItems(cities, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ Toast.makeText(MainActivity.this, "你选择的城市为:" + cities[which], Toast.LENGTH_SHORT).show(); } }); builder.show();
2.单选框列表
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("请选择性别"); final String[] sex = {"男", "女"}; //第二个参数指定默认哪个单选框被勾中 builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ Toast.makeText(MainActivity.this, "性别为:" + sex[which], Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ } }); builder.show();
3.多选框列表
String str; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("选择你想看的电视剧"); final String[] hobbies = {"廷禧攻略", "扶摇", "香蜜沉沉烬如霜", "如懿传"}; //第二个参数代表哪几个选项被选择,需要传递一个boolean[]数组进去,其长度要和第一个参数的长度相同,如果null表示都不选 builder.setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener(){ @Override public void onClick(DialogInterface dialog, int which, boolean isChecked){ if(isChecked){ str.append(hobbies[which] + ", "); } Toast.makeText(MainActivity.this, "选择的是:" + str.toString(), Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ } }); builder.show();
4.等待对话框
ProgressDialog waitingDialog= new ProgressDialog(MainActivity.this); waitingDialog.setTitle("等待加载中,请稍后..."); waitingDialog.setMessage("等待中..."); waitingDialog.setIndeterminate(true);//采用不确定式的进度条 waitingDialog.setCancelable(false);//点击外部不取消对话框 waitingDialog.show();
5.进度条对话框
int MAXPD = 100; ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setProgress(0);//设置默认值 progressDialog.setTitle("正在下载"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//进度条样式 progressDialog.setMax(MAXPD);//设置最大值 progressDialog.show();
自定义Dialog
1.继承Dialog
public class CustomDialog extends Dialog { //标题 protected TextView hintTv; //左边按钮 protected Button doubleLeftBtn; //右边按钮 protected Button doubleRightBtn; //输入框 public EditText editText; public CustomDialog(Context context) { super(context, R.style.CustomDialogStyle); } @Override protected void onCreate(Bundle savedInstanceState) { 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); } //置右键文字和点击事件 public void setRightButton(String rightStr, View.OnClickListener clickListener) { doubleRightBtn.setOnClickListener(clickListener); doubleRightBtn.setText(rightStr); } //设置左键文字和点击事件 public void setLeftButton(String leftStr, View.OnClickListener clickListener) { doubleLeftBtn.setOnClickListener(clickListener); doubleLeftBtn.setText(leftStr); } //设置提示内容 public void setHintText(String str) { hintTv.setText(str); hintTv.setVisibility(View.VISIBLE); } //给两个按钮 设置文字 public void setBtnText(String leftStr, String rightStr) { doubleLeftBtn.setText(leftStr); doubleRightBtn.setText(rightStr); } }
2.自定义Style
3.自定义布局
4.ipad隐藏底部虚拟按键
//弹出dialog时隐藏底部虚拟按键 dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { int uiOptions =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
CustomDialog dialog = new CustomDialog(this); dialog.setHintText("请输入密码"); dialog.setLeftButton("取消", new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.setRightButton("确定", new View.OnClickListener() { @Override public void onClick(View v) { } }); dialog.show();
总结
以上所述是小编给大家介绍的Android Dialog对话框实例代码讲解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款