I have a dialog,开发者_运维技巧 which works just fine for me, but I want to set my background for two buttons in this dialog. The structure of it is quite complicated so I don't want to rewrite it to a custom dialog. But in this case, is there any possibility to set a background(more specifically, is there a way to set a style to positive/negative/neutral buttons)?
Fundamentally you want to access the dialog buttons: these (on the standard AlertDialog) currently have ids android.R.id.button1
for positive, android.R.id.button2
for negative, and android.R.id.button3
for neutral.
So for example to set the background image on the neutral button you can do this:
Dialog d;
//
// create your dialog into the variable d
//
((Button)d.findViewById(android.R.id.button3)).setBackgroundResource(R.drawable.new_background);
EDIT: this is if you are using an AlertDialog.Builder to create it. For all I know these button assignments may change in the future, so keep that in mind.
EDIT: The chunk of code below should generate something that looks like what you want. It turns out you have to call show BEFORE you change the background
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("TEST MESSAGE)
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
((Button)alert.findViewById(android.R.id.button1)).setBackgroundResource(R.drawable.button_border);
Actually there's a better and more reliable way to get the buttons for the Dialog
than the one posted by @Femi .
You can use the getButton method:
Button positiveButton = yourDialog.getButton(DialogInterface.BUTTON_POSITIVE);
The DialogInterface provides all the needed constants:
DialogInterface.BUTTON_POSITIVE
DialogInterface.BUTTON_NEUTRAL
DialogInterface.BUTTON_NEGATIVE
If you want to keep the standard text buttons and change the background of the buttons and the whole dialog you can define your own dialog theme in styles.xml
like this:
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/colorBackground</item>
</style>
...and then when you build the dialog to set its background like this:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyDialogTheme);
Please let me note that this would be bad practice to override the show() method for such problem.
In order to encapsulate the solution into the creation of the dialog itself, best practice is to have the buttons customization within dialog constructor:
class CustomDialog extends AlertDialog
{
public CustomDialog(final Context context)
{
super(context);
setOnShowListener(new OnShowListener()
{
@Override
public void onShow(DialogInterface dialog)
{
Button negativeButton = getButton(DialogInterface.BUTTON_NEGATIVE);
Button positiveButton = getButton(DialogInterface.BUTTON_POSITIVE);
negativeButton.setBackgroundColor(Color.GREEN);
positiveButton.setBackgroundColor(Color.RED);
}
}
}
}
AlertDialog alert = builder.create();
alert.show();
Button bn = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
bn.setBackgroundColor(Color.RED);
Button bp = alert.getButton(DialogInterface.BUTTON_POSITIVE);
bp.setBackgroundColor(Color.YELLOW);
精彩评论