gpt4 book ai didi

java - 如何在editText中显示交替的图标?

转载 作者:行者123 更新时间:2023-12-02 13:43:46 27 4
gpt4 key购买 nike

所以我有带有关闭和成功图标以及最大密码为6的editText,我要做的第一件事是,如果密码不是最大6,则图标保持关闭状态,就像我画的一样,如果密码是最大6,那么关闭图标将变为成功图标,我的问题是如果最多满足6个条件,如何将关闭图标更改为成功图标

editTest image

这是我的代码

 <com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputPassword"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:hint="Password"
app:counterEnabled="true"
app:counterMaxLength="6"
app:endIconMode="clear_text"
app:endIconDrawable="@drawable/ic_canceles"
app:errorEnabled="true"
app:endIconTint="#DF0000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputEmail">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:maxLines="1"
android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

最佳答案

在您的on create方法中尝试

TextInputLayout editText = findViewById(R.id.textInputPassword);


editText.getEditText().addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 6) {
editText.setEndIconDrawable(R.drawable.success);
} else {
editText.setEndIconDrawable(R.drawable.ic_canceles);
}
}

@Override
public void afterTextChanged(Editable s) {

}
});

关于java - 如何在editText中显示交替的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61000041/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com