【问题标题】:EditText drawable hides after setting tint programmatically以编程方式设置色调后 EditText 可绘制隐藏
【发布时间】:2018-04-10 05:32:46
【问题描述】:

我的布局中有两个EditText。我正在使用TextInputLayout 并添加了drawableLeft,现在我正在尝试更改它的色调,这对于用户名字段来说效果很好,但它隐藏了密码字段的左侧可绘制图标。以下是代码,请指导我做错了什么。

<LinearLayout
    android:layout_width="@dimen/margin_300"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical">



<android.support.design.widget.TextInputLayout
                                android:id="@+id/userIDLayout"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_toEndOf="@+id/appCompatImageView4">

                                <android.support.v7.widget.AppCompatEditText
                                    android:id="@+id/edit_user_id"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:drawableLeft="@drawable/ic_user_login"
                                    android:drawablePadding="@dimen/margin_10"
                                    android:drawableStart="@drawable/ic_user_login"
                                    android:drawableTint="@color/colorPrimaryDark"
                                    android:hint="@string/txt_user_hint"
                                    android:imeOptions="flagNoExtractUi|actionNext"
                                    android:inputType="number"
                                    android:maxLines="1"
                                    android:textSize="@dimen/txt_size_16" />
                            </android.support.design.widget.TextInputLayout>


                            <android.support.design.widget.TextInputLayout
                                android:id="@+id/userPasswordLayout"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_toEndOf="@+id/appCompatImageView5"
                                app:passwordToggleEnabled="true">

                                <android.support.design.widget.TextInputEditText
                                    android:id="@+id/edit_user_password"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:drawableLeft="@drawable/ic_user_password"
                                    android:drawablePadding="@dimen/margin_10"
                                    android:drawableStart="@drawable/ic_user_password"
                                    android:drawableTint="@color/colorPrimaryDark"
                                    android:hint="@string/txt_pin_hint"
                                    android:imeOptions="flagNoExtractUi|actionDone"
                                    android:inputType="numberPassword"
                                    android:maxLength="4"
                                    android:maxLines="1"
                                    android:textSize="@dimen/txt_size_16" />
                            </android.support.design.widget.TextInputLayout>

                        </LinearLayout>

以下是我用来以编程方式更改色调的代码:

fun changeEditTextDrawableColor(colorId: Int, drawableId: Int, resource: EditText) {
    var drawable = resources.getDrawable(drawableId)
    drawable = DrawableCompat.wrap(drawable)
    DrawableCompat.setTint(drawable, resources.getColor(colorId))
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN)

    resource!!.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
}

【问题讨论】:

  • 您能否尝试通过从您的密码编辑文本中删除 android:inputType="numberPassword" 来尝试,如果它有效,那么我可以建议您解决方法
  • 如果我删除app:passwordToggleEnabled="true",它工作正常。我测试了它。我想问题正在发生,因为密码切换
  • 不确定 app:"property" 是否可以在运行时设置,但它可以帮助您解决问题
  • 您介意是否可以详细说明。
  • 尝试将inValidate() 拨打至View

标签: android android-layout android-edittext kotlin


【解决方案1】:

问题在于setCompoundDrawablesWithIntrinsicBounds 将其更改为setCompoundDrawablesRelativeWithIntrinsicBounds,它将开始出现。

【讨论】:

  • 此解决方案解决了我试图以编程方式(无色调)将可绘制对象设置到 AutoCompleteTextView 上的问题。谢谢!
猜你喜欢
  • 2017-12-30
  • 2023-03-02
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 2012-07-07
  • 2013-11-07
  • 2015-04-02
  • 2016-11-10
相关资源
最近更新 更多