Jak mogę zmienić kolor przycisku (ów) w urządzeniu AlertDialog
w systemie Android?Zmiana koloru przycisku w AlertDialog
Odpowiedz
Czy odnosisz się do przycisków neutralnych, dodatnich i ujemnych? A może do przycisków zawartych w układzie?
Jeśli odnosisz się do pierwszego, to tak, możesz. Sprawdź numer Custom Button section in this tutorial. Zasadniczo potrzebujesz pliku XML, który wskaże Twojemu przyciskowi, który program do rysowania/koloru użyje dla każdej zmiany stanu. Następnie możesz ustawić ten plik XML jako tło dla przycisku.
Nie budy, już to zrobiłem. Ale muszę zmienić tło przycisku dodatniego, neutralnego i negatywnego. . . –
Witam, udało ci się rozwiązać ten problem? – jonney
Nie można zmienić koloru lub obrazów lub tła domyślnych przycisków pól alertów. W celu dostosowania należy wprowadzić niestandardowe okno dialogowe w ten sposób.
public class TryAgainAlert extends Dialog implements OnClickListener
{
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
Intent i = new Intent(getApplicationContext(), MainMenu.class);
finish();
startActivity(i);
return true;
}
return super.onKeyDown(keyCode, event);
}
TextView scores;
Button tryagain,mainmenu,submit;
public TryAgainAlert(Context context) {
super(context);
setContentView(R.layout.tryagainalert);
scores=(TextView)findViewById(R.id.text);
tryagain= (Button) findViewById(R.id.trya);
mainmenu= (Button) findViewById(R.id.submitscore);
submit= (Button) findViewById(R.id.mainmenu);
}
@Override
public void onClick(View v) {
if(v == tryagain)
{
else if (v==mainmenu)
{
}
else if (v == submit)
{
}
}
}
możesz zrobić, co chcesz z pliku XML. Mam nadzieję, że to pomoże. Dzięki
Oto jak to zrobiłem.
AlertDialog.Builder customBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this,android.R.style.Theme_Dialog));
customBuilder.setTitle(R.string.popup_error_title);
customBuilder.setNegativeButton("Exit application", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyActivity.this.finish();
}
});
AlertDialog dialog = customBuilder.create();
dialog.show();
Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button));
}
uważam odkształcalne here
Witam, stosuję również obraz BG w guziku, taki jak ten. ale jeśli zastosuję obraz, otrzymuję niechciany margines u dołu przycisku. jeśli używam BG kolor i działa dobrze. Sprawdziłem obraz przycisku. w porządku. więc możesz zaproponować jakieś rozwiązanie. – Raj
@Raj znalazłeś rozwiązanie dla tego niechcianego marginesu? – Deepak
@Deepak: jeszcze nie znaleziono żadnego rozwiązania. jeśli znalazłeś, proszę daj mi znać. – Raj
Oto przykład:
AlertDialog.Builder b = new AlertDialog.Builder(all.this);
b.setMessage("r u wan't 2 exit");
b.setCancelable(false);
b.setNegativeButton("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
b.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i=new Intent(getBaseContext(), s.class);
startActivity(i);
}
});
AlertDialog a=b.create();
a.show();
Button bq = a.getButton(DialogInterface.BUTTON_NEGATIVE);
bq.setBackgroundColor(Color.BLUE);
Ponieważ większość ludzi prawdopodobnie przy użyciu DialogFragment teraz wpadłem na niektóre kwestie i kliknięciu moją drogę przez kilka SO odpowiedzi, aby je rozwiązać. Pozwól, że opublikuję moje obecne rozwiązanie.
Skończyło się na ustawianiu tła przycisku z niestandardowymi rysunkami, jak już sugerowano kilka razy. Jednak nie było to jeszcze możliwe w metodzie onCreateDialog
-metoda . Możesz to zrobić np. w onStart()
lub (co jest tym, co preferowałem) w onShow
-listenerie okna dialogowego! Pamiętaj jednak, że musisz następnie unieważnić przyciski po zmianach.
Jeśli chodzi o marginesy: po prostu usuń dopełnienie w swoim Drawable-XML dla przycisków.
#onCreateDialog w DialogFragment:
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// setup your dialog here...
builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
// do something
}
});
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
// do something
}
});
final AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE);
// this not working because multiplying white background (e.g. Holo Light) has no effect
//negativeButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
final Drawable negativeButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_red);
final Drawable positiveButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_green);
if (Build.VERSION.SDK_INT >= 16) {
negativeButton.setBackground(negativeButtonDrawable);
positiveButton.setBackground(positiveButtonDrawable);
} else {
negativeButton.setBackgroundDrawable(negativeButtonDrawable);
positiveButton.setBackgroundDrawable(positiveButtonDrawable);
}
negativeButton.invalidate();
positiveButton.invalidate();
}
});
return dialog;
}
rozciągliwej-XML przykład dla przycisku:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/alert_dialog_button_green_pressed1"
android:endColor="@color/alert_dialog_button_green_pressed2"
android:angle="270" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="@color/alert_dialog_button_green_focused1"
android:startColor="@color/alert_dialog_button_green_focused2"
android:angle="270" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="@color/alert_dialog_button_green1"
android:startColor="@color/alert_dialog_button_green2"
android:angle="270" />
</shape>
</item>
</selector>
Nie zapomnij zdefiniować kolory w res\values\colors.xml
, na przykład tak (nie chciałem gradient, dlatego kolory 1 & 2 są takie same):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="alert_dialog_button_green1">#b4099930</color>
<color name="alert_dialog_button_green2">#b4099930</color>
<color name="alert_dialog_button_green_focused1">#96099930</color>
<color name="alert_dialog_button_green_focused2">#96099930</color>
<color name="alert_dialog_button_green_pressed1">#96099930</color>
<color name="alert_dialog_button_green_pressed2">#96099930</color>
</resources>
Myślę, że istnieją deweloper, którzy chcą rozszerzyć klasę AlertDialog i zdefiniować przyciski kolorów withing definicję klasy.Do tego celu można użyć tego kodu:
class MyDialog extends AlertDialog {
public MyDialog(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);
}
});
}
}
Zrobiłem przez ten kod to może pomóc:
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setCancelable(true);
builder1.setTitle("abc");
builder1.setMessage("abcdefg");
builder1.setInverseBackgroundForced(true);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
Button buttonbackground = alert11.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonbackground.setBackgroundColor(Color.BLUE);
Button buttonbackground1 = alert11.getButton(DialogInterface.BUTTON_POSITIVE);
buttonbackground1.setBackgroundColor(Color.BLUE);
aby zmienić kolor przyciski Kodeksu AlertDailog
:
// Initialize AlertDialog & AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setTitle(R.String.AlertDialogTitle);
...........
.........
//Build your AlertDialog
AlertDialog Demo_alertDialog= builder.create();
Demo_alertDialog.show();
//For Positive Button:
Button b_pos;
b_pos=Demo_alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if(b_pos!=null){
b_pos.setTextColor(getResources().getColor(R.color.YourColor));
}
//For Neutral Button:
Button b_neu;
b_neu=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
if(b_neu!=null){
b_neu.setTextColor(getResources().getColor(R.color.YourColor));
}
//For Negative Button:
Button b_neg;
b_neg=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b_neg!=null){
b_neg.setTextColor(getResources().getColor(R.color.YourColor));
}
Kolor przycisków i innych tekstów można również zmienić za pomocą aplikacji appcompat:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorPrimary">@color/flexdrive_blue_1</item>
<item name="android:textColorPrimary">@color/flexdrive_blue_6</item>
<item name="android:colorAccent">@color/flexdrive_blue_1</item>
<item name="colorPrimaryDark">@color/flexdrive_blue_4</item>
</style>
jeśli używasz DialogFragment (android.app.DialogFragment) wtedy można nadpisać onStart sposób, aby uzyskać uchwyt wszystkich przycisków (pozytywne, negatywne i neutralne).
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View eventEditDialogView = View.inflate(this.getActivity(), R.layout.event_edit_dialog,
null);
builder.setTitle(getLocalizedString("edit_event"))
.setView(eventEditDialogView)
.setPositiveButton(getLocalizedString("all_events"), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(getLocalizedString("this_event"), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
return builder.create();
}
@Override
public void onStart() {
super.onStart();
Button positive = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
positive.setTextColor(Color.BLACK);
positive.setBackgroundColor(getResources().getColor(R.color.GrayBGColor));
}
Wszystkie powyższe rozwiązania będą działać z AlertDialog lub dialogiem utworzonym na tym samym działaniu lub fragmentem, ale nie na DialogFragment utworzonym osobno.
Chciałem rozwiązać ten problem za pomocą motywów, a nie dodatkowego kodu, ponieważ czuję się czystszy, aby mieć wszystkie elementy związane z stylizacją w pliku styles.xml. To, co zrobiłem było oparte na odpowiedź Arade i this other question:
<style name="AlertDialogDanger" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/error</item>
</style>
to zmieni kolor tekstu przycisku dowolnego okna alertu tworzonego stylu AlertDialogDanger
. Aby to zrobić:
new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogDanger))
.setMessage("Really delete?")
.setPositiveButton("Delete", null)
.setNegativeButton("Cancel", null)
.create().show();
możemy zmienić przycisk dialogowe alertu kolor tekstu przy użyciu stylu.
AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.yourDialog);
dialog.setTitle(R.string.title);
dialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//code here
}
});
dialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//do here
}
});
dialog.show();
Style.xml
<style name="yourDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorAccent">@color/themeColor</item>
<item name="android:colorPrimary">@color/themeColor</item>
</style>
//el resto
AlertDialog a=alertDialog.create();
cambiar_color_texto_alertdialog(a);
}
public void cambiar_color_texto_alertdialog(AlertDialog a){
a.show();
Button BN = a.getButton(DialogInterface.BUTTON_NEGATIVE);
BN.setTextColor(parseColor("#2E9AFE"));
Button BA = a.getButton(DialogInterface.BUTTON_POSITIVE);
BA.setTextColor(parseColor("#2E9AFE"));
}
- 1. Zmiana koloru przycisku AlertDialog.Builder
- 2. zmiana koloru przycisku reaguje natywnie
- 3. Zmiana koloru tekstu przycisku po naciśnięciu przycisku
- 4. jQuery zmiana koloru przycisku onclick
- 5. Zmiana koloru przycisku po najechaniu myszą
- 6. Zmiana koloru przycisku akcji na błyszczącym
- 7. Zmiana koloru tła przycisku SearchView i zapytania
- 8. Android AlertDialog tytuł koloru tła
- 9. Zmiana koloru tła przycisku za pomocą wzoru MVVM w WPF
- 10. Zmiana koloru przycisku po najechaniu myszą w Xaml
- 11. Zmiana koloru tła UIButton
- 12. Zmiana rozmiaru czcionki na AlertDialog
- 13. Zmiana rozmiaru EditText wewnątrz AlertDialog
- 14. Zmiana koloru wskaźnika na przycisku z dostosowywaniem Bootstrap
- 15. Zmiana koloru tła przycisku paska zadań po naciśnięciu
- 16. Zmiana koloru JButton w JGoodies WindowsLookAndFeel
- 17. Zmiana koloru tekstu w WinForms RichTextBox
- 18. zmiana koloru tła układu w Androidzie
- 19. Zmiana koloru składni w Scintilla.NET
- 20. Zmiana koloru tyldy (~) w Vim
- 21. Zmiana koloru kostki w three.js
- 22. Zmiana koloru svg w qt
- 23. Zmiana koloru etykiet w JFreeChart
- 24. Zmiana koloru klawiatury w iphone
- 25. Zmiana koloru tekstu w MoreNavigationController
- 26. Zmiana koloru tekstu w Android.R.layout.simple_list_item_2
- 27. Zmiana koloru obrazu w javascript
- 28. Zmiana koloru paska NSSlider
- 29. Zmiana koloru selektora Preferencji
- 30. JLabel zmiana koloru
Spójrz na ten [Android Dialog z niestandardowych kolorów i wzorów] (http://stackoverflow.com/a/30388711/782535) . –