很多时候,系统生成的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() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
// 确定按钮
Button btnOK = (Button) layout.findViewById(R.id.previousBtn);
btnOK.setOnClickListener(new OnClickListener() {
@Override
public 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() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
这个时候,我们只要设置好xml里面的布局,使其满足我们的需求就可以了。。很简单吧!!!