アクティビティがテーマを設定すると、AlertDialog ボタンのテキストが表示されなくなります

	<activity
	   android:name=".modules.other.SplashActivity"
	   android:exported="true"
	   android:screenOrientation="portrait"
	   android:theme="@style/SplashBackgroundStyle">
	</activity>
    <style name="SplashBackgroundStyle" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/app_launch_bg</item>
        <item name="android:windowFullscreen">true</item>
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryVariant">@color/white</item>
        <item name="colorOnPrimary">@color/white</item>
    </style>

理由:
カスタム テーマでフォントの色が白に設定されているため、AlertDialog ボタンのテキストが表示されませんが、ボタンの機能は引き続き存在します。

方法 1:

show()の後にボタンのフォントの色を設定します。

     AlertDialog dialog = new AlertDialog.Builder(nActivity).setCancelable(false)
             .setTitle("提示")
             .setMessage("安装应用需要打开未知来源权限,请去设置中开启权限")
             .setNegativeButton("取消", null)
             .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialogInterface, int i) {
                     startInstallPermissionSettingActivity();
                 }
             }).create();
     dialog.show();
     
     // 设置按钮字体颜色
     Button btnPos = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
     btnPos.setTextColor(Color.RED);
     Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
     btnNeg.setTextColor(Color.BLACK);

方法 2:

AlertDialog はカスタム スタイルを使用します。

<style name="MyDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
	<item name="colorPrimary">@color/colorPrimary</item>
	<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
	<item name="colorAccent">@color/colorAccent</item>
</style>
AlertDialog dialog = new AlertDialog.Builder(nActivity, R.style.MyDialog).setCancelable(false)
        .setTitle("提示")
        .setMessage("安装应用需要打开未知来源权限,请去设置中开启权限")
        .setNegativeButton("取消", null)
        .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                startInstallPermissionSettingActivity();
            }
        }).create();
dialog.show();

おすすめ

転載: blog.csdn.net/zhijiandedaima/article/details/131046051