Programming

android 5에서 기본 대화 상자 텍스트 색상을 변경하는 방법

procodes 2020. 7. 10. 21:43
반응형

android 5에서 기본 대화 상자 텍스트 색상을 변경하는 방법


내 앱에 많은 경고 대화 상자가 있습니다. 기본 레이아웃이지만 대화 상자에 양수 및 음수 버튼을 추가하고 있습니다. 따라서 버튼은 기본 텍스트 색상 인 Android 5 (녹색)를 얻습니다. 나는 성공하지 않고 그것을 바꾸려고 노력했다. 해당 텍스트 색상을 변경하는 방법에 대한 아이디어가 있습니까?

내 사용자 정의 대화 상자 :

public class MyCustomDialog extends AlertDialog.Builder {

    public MyCustomDialog(Context context,String title,String message) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View viewDialog = inflater.inflate(R.layout.dialog_simple, null, false);

        TextView titleTextView = (TextView)viewDialog.findViewById(R.id.title);
        titleTextView.setText(title);
        TextView messageTextView = (TextView)viewDialog.findViewById(R.id.message);
        messageTextView.setText(message);

        this.setCancelable(false);

        this.setView(viewDialog);

    } }

대화 상자 만들기 :

MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", errorMessage);
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ...
                        }
}).show();

음수 버튼은 기본 대화 상자이며 Android 5 Lollipop에서 기본 녹색을 사용합니다.

많은 감사

녹색 버튼이있는 사용자 정의 대화 상자


당신은 만들려고 할 수 있습니다 AlertDialog첫 번째 개체를 선택한 후 버튼의 색상을 변경 설정을 사용하여 다음을 보여줍니다. ( builder객체를 호출하는 대신 객체를 show()호출 create()하기 위해 호출 AlertDialog합니다.

//1. create a dialog object 'dialog'
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", errorMessage); 
AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    ...
                }

            }).create();

//2. now setup to change color of the button
dialog.setOnShowListener( new OnShowListener() {
    @Override
    public void onShow(DialogInterface arg0) {
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(COLOR_I_WANT);
    }
});

dialog.show()

The reason you have to do it on onShow() and cannot just get that button after you create your dialog is that the button would not have been created yet.

I changed AlertDialog.BUTTON_POSITIVE to AlertDialog.BUTTON_NEGATIVE to reflect the change in your question. Although it is odd that "OK" button would be a negative button. Usually it is the positive button.


Here's a natural way to do it with styles:

If your AppTheme is inherited from Theme.MaterialComponents, then:

<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>

<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#f00</item>
</style>

<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#00f</item>
</style>

If your AppTheme is inherited from Theme.AppCompat:

<style name="AlertDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert">
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>

<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">#f00</item>
</style>

<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">#00f</item>
</style>

Use your AlertDialogTheme in your AppTheme

<item name="alertDialogTheme">@style/AlertDialogTheme</item>

or in constructor

androidx.appcompat.app.AlertDialog.Builder(context, R.style.AlertDialogTheme)

The color of the buttons and other text can also be changed via theme:

values-21/styles.xml

<style name="AppTheme" parent="...">
  ...
  <item name="android:timePickerDialogTheme">@style/AlertDialogCustom</item>
  <item name="android:datePickerDialogTheme">@style/AlertDialogCustom</item>
  <item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
</style>

<style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert">
  <item name="android:colorPrimary">#00397F</item>
  <item name="android:colorAccent">#0AAEEF</item>
</style>

The result:

대화 날짜 선택기


The simpliest solution is:

dialog.show(); //Only after .show() was called
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(neededColor);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(neededColor);

There are two ways to change the dialog button color.

Basic Way

If you just want to change in an activity, write the below two lines after alertDialog.show();

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark));

Recommended

I'll recommend adding a theme for AlertDialog in styles.xml so you don't have to write the same code again and again in each activity/dialog call. You can just create a style and apply that theme on the dialog box. So whenever you want to change the color of AlertDialog box, just change color in styles.xml and all the dialog boxes will be updated in the whole application.

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorPrimary</item>
</style>

And apply the theme in AlertDialog.Builder

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);

If you want to change buttons text color (positive, negative, neutral) just add to your custom dialog style:

<item name="colorAccent">@color/accent_color</item>

So, your dialog style must looks like this:

<style name="AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColor">@android:color/black</item>
    <item name="colorAccent">@color/topeka_accent</item>
</style>

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:colorPrimary">#00397F</item>
    <item name="android:textColorPrimary">#22397F</item>
    <item name="android:colorAccent">#00397F</item>
    <item name="colorPrimaryDark">#22397F</item>
</style>

The color of the buttons and other text can also be changed using appcompat :


Just as a side note:

버튼의 색상 (및 전체 스타일)은 현재 테마에 따라 달라지며 사용하면 다소 다를 수 있습니다

     android.app.AlertDialog.Builder builder = new AlertDialog.Builder()

또는

    android.support.v7.app.AlertDialog.Builder builder = new AlertDialog.Builder()

(더 나은 것을 사용하십시오)


나를 위해 그것은 다른 버튼 테마를 사용

<style name="ButtonLight_pink" parent="android:Widget.Button">
      <item name="android:background">@drawable/light_pink_btn_default_holo_light</item>
      <item name="android:minHeight">48dip</item>
      <item name="android:minWidth">64dip</item>
      <item name="android:textColor">@color/tab_background_light_pink</item>
    </style>

때문에

android : textColor

나는 거기에 흰색이었다. .. 나는 어떤 버튼 텍스트도 보지 않았다 (대화 버튼은 기본적으로 버튼이기도하다) 우리는 가서 수정하고 고쳤습니다.

참고 URL : https://stackoverflow.com/questions/27965662/how-can-i-change-default-dialog-button-text-color-in-android-5

반응형