【问题标题】:How to change the border color(un-focused) of an EditText?如何更改 EditText 的边框颜色(未聚焦)?
【发布时间】:2012-03-02 17:34:24
【问题描述】:

我将 EditText 的背景颜色更改为透明。现在 EditText 在不聚焦时看起来不可见。那么如何更改 EditText 的未聚焦边框颜色呢?

这个的 XML 属性是什么?

【问题讨论】:

    标签: android colors android-edittext border


    【解决方案1】:

    在drawable中创建一个XML文件(比如backwithborder.xml):

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#00000000" />
        <stroke android:width="1dip" android:color="#ffffff" />
    </shape>
    

    对于 EditText 用户属性android:background="@drawable/backwithborder"

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      当用户关注 EditText 时,Android OS 本身会为其添加边框。颜色取决于操作系统版本。有时我们可能想摆脱默认的焦点边框,但有办法做到这一点。

      我们可以保持背景颜色为透明以移除焦点上的 EditText 边框。

           <EditText 
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:background="#00000000"
            />
      

      【讨论】:

      • 问题是关于未聚焦状态,而不是聚焦状态。
      【解决方案4】:

      回答太晚了,但我为新开发人员编写了这段代码:

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_focused="true">
          <layer-list>
              <item>
                  <shape android:shape="rectangle">
                      <stroke android:width="2dp" android:color="@color/components_colorPrimaryDark" />
                      <solid android:color="@color/components_white" />
                  </shape>
              </item>
      
              <item android:bottom="2dp">
                  <shape android:shape="rectangle">
                      <solid android:color="@color/components_white" />
                  </shape>
              </item>
          </layer-list>
      </item>
      <item android:state_focused="false">
          <layer-list>
              <item>
                  <shape android:shape="rectangle">
                      <stroke android:width="1dp" android:color="@color/components_colorAccent" />
                      <solid android:color="@color/components_white" />
                  </shape>
              </item>
      
              <item android:bottom="1dp">
                  <shape android:shape="rectangle">
                      <solid android:color="@color/components_white" />
                  </shape>
              </item>
          </layer-list>
      </item>
      

      【讨论】:

      • 很好的答案@jordiz,正是我需要的。
      【解决方案5】:

      如果只想更改 EditText 边框颜色,可以使用 attr app:backgroundTint

      <EditText
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:backgroundTint="#000"
      /> 
      

      【讨论】:

        【解决方案6】:

        您可以创建一个带有背景颜色的线性布局,该颜色是您希望边框应为的颜色。然后将编辑文本放在这个Linearlayout 中,并提供一些背景颜色。 例如:如果边框为灰色,则Linearview 的背景为灰色。 并编辑文本背景黑色。

        对于边框的宽度,您可以为border_width = "1dp" 提供padding = "1dp"

        【讨论】:

          猜你喜欢
          • 2014-06-18
          • 1970-01-01
          • 2019-09-28
          • 2019-02-10
          • 2019-08-22
          • 1970-01-01
          • 2019-02-03
          • 1970-01-01
          • 2021-01-11
          相关资源
          最近更新 更多