EditText选择的文本范围指针有奇怪的背景

EditText selected text range pointer got strange background

编辑文本样式

    <style name="MyEditTextStyle" parent="Widget.AppCompat.EditText">
            <item name="android:textCursorDrawable">@null</item>
            <item name="android:textColor">@color/color_states_blackish</item>
            <item name="android:fontFamily">@font/sfd_medium</item>
            <item name="android:background">@drawable/edit_white_gray_x_normal_rounded</item>
            <item name="android:height">@dimen/general_view_height</item>
        </style>

对话框样式

 <style name="MyAlertDialogTheme" parent="Theme.AppCompat.Light.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">@color/text_color_blackish</item>
        <item name="android:fontFamily">@font/sfd_regular</item>
    </style>

    <style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
        <item name="android:textColor">@color/text_color_blackish</item>
        <item name="android:fontFamily">@font/sfd_bold</item>
    </style>

对话框代码

final EditText editText = new EditText(new android.view.ContextThemeWrapper(getContext(),R.style.MyEditTextStyle));
                        final FrameLayout layout = new FrameLayout(getContext());
                        int padding = getContext().getResources().getDimensionPixelSize(R.dimen.standard_wall_space);
                        int paddingView = getContext().getResources().getDimensionPixelSize(R.dimen.standard_view_space);
                        layout.setPadding(padding,paddingView,padding,paddingView);
                        layout.addView(editText);
                        editText.setHint(R.string.type_here);
                        new AlertDialog.Builder(getContext(),R.style.MyAlertDialogTheme)
                        .setTitle("Enter Your Unit")
                        .setView(layout)
                        .setPositiveButton("Add", (dialog, whichButton) -> {
                            String value = editText.getText().toString();
                            MyApp.showToast("Unit-"+value);
                        })
                        .setNegativeButton("Cancel", null).show();

我得到了以下与 EditText 背景相同的指针,我删除了它们 ContextThemeWrapper 然后它显示正确但我需要使用样式更改背景?,为什么会这样?这是全屏对话框中的对话框?任何解决方案?谢谢

看起来像 bug,选择器弹出窗口 window 背景样式错误,请勿使用 ContextThemeWrapper,样式会正常显示,以编程方式设置背景。

 final EditText editText = new EditText(getContext());
editText.setBackgroundResource(R.drawable.edit_white_gray_x_normal_rounded);
editText.setTextColor(ContextCompat.getColor(getContext(),R.color.color_states_blackish));

问题是您主题中的 android:background 参数应用于 EditText 中的所有内容。对我来说,解决方案是从样式中删除该参数。

除了这里的其他答案外,我们在style.xml中的应用主题中设置背景时也会出现这种情况。我找到的另一个解决方案是将 android:background 替换为背景颜色的 android:backgroundTint 并将 android:backgroundTintMode 设置为 multiple.

将其放入样式中:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="android:backgroundTint">#FFFFFF</item>
    <item name="android:backgroundTintMode">multiply</item>
</style>

或在您要更改其背景颜色的视图标签中:

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="#FFFFFF"
    android:backgroundTintMode="multiply" />