问题
Is possible to customize positive and negative buttons in AlertDialog ? I need to replace default look of positive and negative with custom.
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {...
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {...
Can somebody tell me how to do that ?
回答1:
public class ComentarDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Mensaje de alerta")
.setTitle("Comentar")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
@Override
public void onStart() {
super.onStart();
//Personalizamos
Resources res = getResources();
//Buttons
Button positive_button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
positive_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));
Button negative_button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
negative_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));
int color = Color.parseColor("#304f5a");
//Title
int titleId = res.getIdentifier("alertTitle", "id", "android");
View title = getDialog().findViewById(titleId);
if (title != null) {
((TextView) title).setTextColor(color);
}
//Title divider
int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
View titleDivider = getDialog().findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(color);
}
}
}
回答2:
if you want to change them, I will suggest using activity with your required layout and add them= Dialog in your activity, in Manifest file
回答3:
if you want to customize you can use dialog instead of alert dialog here is the sample code
final Dialog dialog = new Dialog(ThisweekActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
View view = LayoutInflater.from(ThisweekActivity.this).inflate(R.layout.issue_cover_prompt_layout, null);
view.findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
ImageView img = (ImageView) view.findViewById(R.id.issue_cover_img);
img.setImageBitmap(issue.getCoverImage());
dialog.setContentView(view);
dialog.show();
you can set set the layout in dialog and add click listner on it
回答4:
Yuo can set every view in dialog box. You can set view with two buttons and dont set positive & negative buttons.
example:
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
View dialogView = LayoutInflater.from(this)
.inflate(R.layout.my_layout, null);
builder.setView(dialogView);
来源:https://stackoverflow.com/questions/8861745/is-possible-to-customize-positive-and-negative-buttons-in-alertdialog