我宁愿不使用警报对话框,但如果我可以将正按钮设置为我已经拥有的按钮,我会的。 如果我不能做到这一点,有没有办法在自定义对话框中设置正按钮和负按钮?
您可以使用以下自定义警报对话框。
public class CustomAlertDialog {
public void showDialog(Context activity, String msg, String buttonText, final CustomDialogListener customDialogListener){ //one button with callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert);
TextView text = (TextView) dialog.findViewById(R.id.text_alertdialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_alert_dialog);
dialogButton.setText(buttonText);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onPositiveButtonClick();
}
});
dialog.show();
}
public void showDialog(Context activity, String msg, String positiveButtonText, String negativeButtonText, final CustomDialogListener customDialogListener){//two button with callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert_two_button);
TextView text = (TextView) dialog.findViewById(R.id.text_alert_two_dialog);
text.setText(msg);
Button positiveDialogButton = (Button) dialog.findViewById(R.id.btn_alert_two_dialog_YES);
Button negativeDialogButton = (Button) dialog.findViewById(R.id.btn_alert_two_dialog_NO);
positiveDialogButton.setText(positiveButtonText);
negativeDialogButton.setText(negativeButtonText);
positiveDialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onPositiveButtonClick();
}
});
negativeDialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onNegativeButtonClick();
}
});
dialog.show();
}
public void showDialog(Context activity, String msg, String buttonText){ //simple alert without callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert);
TextView text = (TextView) dialog.findViewById(R.id.text_alertdialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_alert_dialog);
dialogButton.setText(buttonText);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
custom_alert.xml包含一个按钮和一个用于显示消息的文本视图。
custom_alert_two_button.xml包含两个按钮和一个用于显示消息的文本视图。
最后一个仅包含一个用于显示消息的文本视图。