在保持线条颜色的同时设置 EditText 的背景

Setting the background of an EditText while keeping the line color

单击编辑按钮后,我更改了 EditText 的外观:

editText.setBackgroundColor(Color.parseColor("#E1E2E3"));
editText.getBackground().setAlpha(80);

结果是:

它删除出现在 EditText 中文本下方的行。我试过添加 editText.getBackground().setColorFilter(Color.parseColor("#DACC3E"), PorterDuff.Mode.SRC_IN); 但这只会改变背景的颜色。有没有办法在保持背景颜色的同时恢复下划线?

只需将 EditText 的背景设置为一种颜色即可覆盖默认背景,即彩色下划线。您必须创建自己的 resizable bitmap 才能用作 EditText.

的背景

虽然@Bryan 的回答可能是官方推荐的方法...您可以使用一些解决方法:只需将 EditText 包裹在 ViewGroup 中并将背景更改应用到ViewGroup 而不是 EditText。例如

<LinearLayout
    android:id="@+id/et__parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

然后应用背景(也可以将 alpha 添加到颜色中):

findViewById(R.id.et__parent)
    .setBackgroundColor(Color.parseColor("#50E1E2E3")); // 50 = 80 in HEX