如何修改Dialog中选中的TextInputEditText及按钮文本颜色?

一、修改激活状态下输入框的颜色
你可以通过两种方式自定义TextInputEditText激活时的颜色(比如蓝绿色/绿色):
方式1:直接在布局的TextInputLayout中设置属性
给每个TextInputLayout添加boxStrokeColor和hintTextColor属性,指定激活状态的自定义颜色:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/til_email_user_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" app:boxBackgroundColor="@android:color/transparent" app:startIconDrawable="@drawable/ic_baseline_email_24" <!-- 新增:设置激活时的边框和提示文字颜色 --> app:boxStrokeColor="@color/your_custom_green" app:hintTextColor="@color/your_custom_green" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tv_login_dialog_title"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/ti_et_email_user_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="bottom" android:hint="@string/field_hint_email" android:inputType="textEmailAddress" /> </com.google.android.material.textfield.TextInputLayout>
提示:
@color/your_custom_green需要你在res/values/colors.xml中预先定义,例如:<color name="your_custom_green">#00C851</color>
方式2:通过对话框主题统一设置
如果希望对话框内所有输入框共享同一自定义颜色,可修改你已有的MaterialAlertDialog_rounded主题,重写Material组件的核心颜色属性:
<style name="MaterialAlertDialog_rounded" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog"> <item name="shapeAppearanceOverlay">@style/DialogCorners</item> <!-- 新增:统一设置输入框激活时的主色调 --> <item name="colorPrimary">@color/your_custom_green</item> </style>
该设置会将对话框内所有依赖colorPrimary的Material组件(包括输入框激活边框、提示文字高亮色)统一替换为你指定的颜色。
二、修改对话框操作按钮的文本颜色
针对"Login"和"Cancel"按钮的文本颜色,同样有两种实现方式:
方式1:在主题中统一配置
继续扩展MaterialAlertDialog_rounded主题,添加按钮文本颜色属性:
<style name="MaterialAlertDialog_rounded" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog"> <item name="shapeAppearanceOverlay">@style/DialogCorners</item> <!-- 若使用了主题统一设置输入框颜色,保留以下属性 --> <item name="colorPrimary">@color/your_custom_green</item> <!-- 新增:设置确认/取消按钮的文本颜色 --> <item name="buttonPositiveTextColor">@color/your_custom_green</item> <item name="buttonNegativeTextColor">@color/your_custom_gray</item> </style>
其中buttonPositiveTextColor控制确认按钮(Login)的颜色,buttonNegativeTextColor控制取消按钮(Cancel)的颜色。
方式2:在代码中动态设置
如果需要根据业务逻辑动态调整按钮颜色,可在对话框创建后获取按钮实例并修改:
@NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { binding = DialogLoginBinding.inflate(getLayoutInflater()); MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(requireActivity(), R.style.MaterialAlertDialog_rounded); dialogBuilder.setView(binding.getRoot()); dialogBuilder.setNegativeButton("Cancel", (dialog, which) -> { Utils.INSTANCE.showLoginCancellationSnackBar(view, getLayoutInflater()); dialog.cancel(); }); dialogBuilder.setPositiveButton("Login", (dialog, which) -> Toast.makeText( getActivity(), "Logged In", Toast.LENGTH_LONG).show()); // 创建对话框后设置按钮颜色 Dialog dialog = dialogBuilder.create(); dialog.setOnShowListener(dialogInterface -> { AlertDialog alertDialog = (AlertDialog) dialog; // 设置确认按钮颜色 alertDialog.getButton(DialogInterface.BUTTON_POSITIVE) .setTextColor(getResources().getColor(R.color.your_custom_green)); // 设置取消按钮颜色 alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE) .setTextColor(getResources().getColor(R.color.your_custom_gray)); }); return dialog; }
注意:确保导入
androidx.appcompat.app.AlertDialog(AndroidX项目)或android.app.AlertDialog(传统项目)。
内容的提问来源于stack exchange,提问作者Klem Lloyd Mwenya

