在Android编辑文本下方获得错误图标,可以通过以下步骤实现:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
TextInputLayout textInputLayout = findViewById(R.id.textInputLayout);
EditText editText = findViewById(R.id.editText);
// 设置错误信息
textInputLayout.setError("输入错误");
// 设置错误图标
textInputLayout.setErrorIconDrawable(R.drawable.ic_error);
editText.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) {
// 文本变化中的操作
}
@Override
public void afterTextChanged(Editable s) {
// 文本变化后的操作
if (s.length() < 5) {
textInputLayout.setError("输入长度不能小于5");
} else {
textInputLayout.setError(null);
}
}
});
通过以上步骤,当EditText中的文本不符合要求时,TextInputLayout会在文本下方显示错误信息和错误图标。
领取专属 10元无门槛券
手把手带您无忧上云