【问题标题】:How to make text grey in TextInputEditText after TextInputLayout enabled = false?启用 TextInputLayout = false 后,如何使 TextInputEditText 中的文本变灰?
【发布时间】:2021-12-21 06:31:55
【问题描述】:

当我禁用我的 TextInputLayout

InputTextContainer.isEnabled = false

它变灰了,因此您可以直观地理解它不可用,但即使它也被阻止,其中的文本仍然是黑色的。我希望我的文本作为禁用的容器是灰色的。你能帮我做吗?

【问题讨论】:

    标签: android xml kotlin


    【解决方案1】:

    使用默认主题,当您禁用 TextInputLayout 时,TextInputLayout 包含的 EditText 中的文本也会变灰。您可能在您的主题或 EditText 上的特定样式中做了一些覆盖此操作的操作。

    例如,如果您指定了这样的文本颜色:

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        tools:hint="Hint"
        tools:text="Text" />
    

    ...您指定了一种颜色,它不能对视图的不同状态做出反应。这里我定义了黑色#000000,所以无论如何文字都是黑色的。

    如果您想要一种在禁用时变为灰色或透明的颜色,您需要定义一个ColorStateList color in XML 并将其用作您的android:textColor。确保 ColorStateList 至少有一个与state_enabled="false" 对应的状态。例如:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:color="#80000000"
            android:state_enabled="false"/> <!-- Translucent black for disabled state -->
        <item
            android:color="#FF000000"/> <!-- Default -->
    </selector>
    

    【讨论】:

      【解决方案2】:

      假设这是你的 xml 视图

      <com.google.android.material.textfield.TextInputLayout
              android:id="@+id/pswrd"
              .....>
      
              <com.google.android.material.textfield.TextInputEditText
                  android:id="@+id/pswrd_text"
                  ....>
      
              </com.google.android.material.textfield.TextInputEditText>
      
          </com.google.android.material.textfield.TextInputLayout>
      

      你可以尝试两件事:

      1. 除了设置pswrd.isEnabled = false,还设置pswrdText.isEnabled=false
      2. 如果 1 不起作用,请尝试 pswrdText.setTextColor(android.R.color.darker_gray)

      【讨论】:

        【解决方案3】:

        按照@Nitin Verma 的回答试试这个方法。

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edt_email">
        
        <TextView
          android:id="@+id/textView20"
          android:layout_width="wrap_content"/>
        
        <com.google.android.material.textfield.TextInputEditText/>
        
        edt_email.isEnable = false
        textView20.isEnable = false
        

        【讨论】:

          猜你喜欢
          • 2017-04-18
          • 1970-01-01
          • 1970-01-01
          • 2016-10-16
          • 2019-02-09
          • 2020-09-28
          • 1970-01-01
          • 2019-10-22
          • 2018-10-13
          相关资源
          最近更新 更多