簡體   English   中英

如何以編程方式使edittext下划線透明白色

[英]How to make edittext underline transparent white programmatically

View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus){
                editText.getBackground().setColorFilter(ContextCompat.getColor(getActivity().getApplicationContext(),R.color.ed_background), PorterDuff.Mode.SRC_ATOP);
            } else {
                int color = Color.parseColor("#FFFFFF");
                editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            }
        }
    };

<color name="ed_background">#33FFFFFF</color>

這使編輯文本下划線為黑色,但我希望它是透明的白色。 使用 xml 設置自定義樣式也會將其設置為黑色。 如何使用java使下划線變成透明的白色?

你可以像這樣嘗試: edittext_lined.xml drawable xml for ex edittext_lined.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-2dp" android:right="-2dp" android:left="-2dp">
    <shape>
      <solid android:color="@android:color/transparent" />
      <stroke android:color="@color/ed_background" android:width="1dp"/>
    </shape>
</item>

然后在代碼中:

editText.setBackgroundResource(R.drawable.edittext_lined);

只需在 xml 布局/代碼中將 edittext 背景顏色設置為白色/透明即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM