android开发之自定义dialog

来源:互联网 发布:上海开票软件 网络配置 编辑:程序博客网 时间:2024/06/04 19:20

很多时候,系统生成的dialog无法满足我们的需求,这个时候,我们就只能自己去自定义一个dialog来满足自己的需要了。


贴上一组代码:


<span style="white-space:pre"></span>/** 设置dialog(双按钮) */private void showDialog(String str1, String str2) { //LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.view_dialog, null);// 对话框final Dialog dialog = new AlertDialog.Builder(BindBankActivity.this).create();dialog.show();dialog.getWindow().setContentView(layout);TextView text0 = (TextView) layout.findViewById(R.id.dialogTitleTxt);text0.setText("请确认以下信息");text0.setVisibility(View.VISIBLE);TextView text1 = (TextView) layout.findViewById(R.id.dialogImage);text1.setBackground(null);text1.setText("姓名:" + str1);text1.setVisibility(View.VISIBLE);TextView text2 = (TextView) layout.findViewById(R.id.dialogContentTxt);text2.setText("银行卡号:" + str2);text1.setVisibility(View.VISIBLE);// 取消按钮Button btnCancel = (Button) layout.findViewById(R.id.nextBtn);btnCancel.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();}});// 确定按钮Button btnOK = (Button) layout.findViewById(R.id.previousBtn);btnOK.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {submitBindBank();dialog.dismiss();}});}


<span style="white-space:pre"></span>/** 设置dialog(单按钮) */private void showBankSupportDialog() {LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.view_dialog_banksupport, null);// 对话框final Dialog dialog = new AlertDialog.Builder(BindBankActivity.this).create();dialog.show();dialog.getWindow().setContentView(layout);// 确定按钮Button btnOK = (Button) layout.findViewById(R.id.bt_banksupport_sure);btnOK.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();}});}

这个时候,我们只要设置好xml里面的布局,使其满足我们的需求就可以了。。很简单吧!!!



1 0
原创粉丝点击