EDIT: This only works on API 8+ as noted by some of the comments.
(编辑:这仅适用于API 8+,如某些注释所述。)
This is a late answer, but you can add an onShowListener to the AlertDialog where you can then override the onClickListener of the button.
(这是一个较晚的答案,但是您可以在AlertDialog中添加onShowListener,然后在其中可以覆盖按钮的onClickListener。)
final AlertDialog dialog = new AlertDialog.Builder(context)
.setView(v)
.setTitle(R.string.my_title)
.setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
.setNegativeButton(android.R.string.cancel, null)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Do something
//Dismiss once everything is OK.
dialog.dismiss();
}
});
}
});
dialog.show();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…