android 5에서 기본 대화 상자 텍스트 색상을 변경하는 방법 LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE

내 앱에 많은 경고 대화 상자가 있습니다. 기본 레이아웃이지만 대화 상자에 양수 및 음수 버튼을 추가하고 있습니다. 따라서 버튼은 기본 텍스트 색상 인 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()

당신이 그것을해야하고 onShow()대화를 만든 후에 해당 버튼을 얻을 수없는 이유 는 버튼이 아직 생성되지 않았기 때문입니다.

나는 변화 AlertDialog.BUTTON_POSITIVE하는 AlertDialog.BUTTON_NEGATIVE질문의 변화를 반영하기 위해. “OK”버튼이 음수 버튼 일 가능성은 희박합니다. 일반적으로 양수 버튼입니다.


답변

스타일로 자연스럽게 처리하는 방법은 다음과 같습니다.

AppTheme에서 상속받은 경우 Theme.MaterialComponents:

<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>

AppTheme에서 상속받은 경우 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>

당신의 AlertDialogTheme에서 사용AppTheme

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

또는 생성자

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

답변

버튼 및 기타 텍스트의 색상은 테마를 통해 변경할 수도 있습니다.

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>

결과:


답변

가장 간단한 해결책은 다음과 같습니다.

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

답변

대화 상자 버튼 색상을 변경하는 방법에는 두 가지가 있습니다.

기본 방법

활동을 변경하려면 다음 두 줄을 적어 두십시오. alertDialog.show();

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

추천

각 활동 / 대화 호출에서 동일한 코드를 반복해서 작성할 필요 AlertDialogstyles.xml없도록에 테마를 추가하는 것이 좋습니다 . 스타일을 만들고 대화 상자에서 해당 테마를 적용하면됩니다. 따라서 AlertDialog 상자의 색상을 변경하려면 styles.xml에서 색상을 변경하면 모든 대화 상자가 전체 응용 프로그램에서 업데이트됩니다.

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

그리고 테마를 적용하십시오 AlertDialog.Builder

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

답변

버튼 텍스트 색상을 변경하려면 (양수, 음수, 중립) 사용자 정의 대화 상자 스타일에 추가하십시오.

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

따라서 대화 스타일은 다음과 같아야합니다.

<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>

appcompat를 사용하여 버튼 및 기타 텍스트의 색상을 변경할 수도 있습니다.