如何在TextInputLayout
中输入一个文字后隐藏EditText
错误。
可能吗?
我怎么能做到这一点,或者我在这里做错了。!!
码
layoutEdtPhone =(TextInputLayout)rootView.findViewById(R.id.layoutEdtPhone);
layoutEdtPhone.setErrorEnabled(true);
layoutEdtPhone.setError(getString(R.string.ui_no_phone_toast));
layoutEdtPassword = (TextInputLayout)rootView.findViewById(R.id.layoutEdtPassword);
layoutEdtPassword.setErrorEnabled(true);
layoutEdtPassword.setError(getString(R.string.ui_no_password_toast));
edtPhone=(EditText)rootView.findViewById(R.id.edtPhone);
edtPassword=(EditText)rootView.findViewById(R.id.edtPassword);
XML
<EditText
android:id="@+id/edtPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="@drawable/edt_background_selector"
android:drawableLeft="@drawable/phone_icon"
android:drawableStart="@drawable/phone_icon"
android:hint="@string/phone"
android:inputType="phone"
android:padding="5dip"
android:singleLine="true"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/layoutEdtPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/edtPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/edt_background_selector"
android:drawableLeft="@drawable/password_icon"
android:drawableStart="@drawable/password_icon"
android:hint="@string/password"
android:inputType="textPassword"
android:padding="5dip"
android:singleLine="true"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
答案 0 :(得分:15)
为了进一步说明Prithviraj给出的答案,TextInputLayout
不进行验证。它只是一种显示错误或提示的机制。您负责设置/清除错误。这是你如何做到这一点。请注意,除了TextChangedListener之外,当用户跳转到第二个编辑文本而不在第一个字段中进行任何修改时,您可能还需要OnFocusChangeListener来设置错误。
protected void onCreate(Bundle savedInstanceState) {
//.....
edtPhone.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) {
validateEditText(s);
}
});
edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
validateEditText(((EditText) v).getText());
}
}
});
}
private void validateEditText(Editable s) {
if (!TextUtils.isEmpty(s)) {
layoutEdtPhone.setError(null);
}
else{
layoutEdtPhone.setError(getString(R.string.ui_no_password_toast));
}
}
}
答案 1 :(得分:11)
您可以设置layoutEdtPhone.setErrorEnabled(false);
答案 2 :(得分:6)
做
mTextInputLayout.setError(null);
清除错误消息。
一个好的做法可以是检查这样的错误的方法:
@Override
public void checkErrorBeforeAction() {
boolean error = false;
mTextInputLayout.setError(null);
if (mEditText.getText().toString().length == 0)) {
mTextInputLayout.setError("Field empty");
}
else if (mEditText.getText().toString().isValid) { // Other condition
mTextInputLayout.setError("Field is invalid");
}
if (!error) {
// Call action
}
}
这样,它会在设置新错误消息之前刷新错误消息。
答案 3 :(得分:5)
我使用ButterKnife的@TextChanged并为我工作,看看:
@Bind(R.id.layoutEdtPhone)
TextInputLayout tlayoutEdtPhone;
@Bind(R.id.edtPhone)
EditText edtPhone;
//start ButterKnife (I spent the URL with full description for initilize)
@OnTextChanged(R.id.edtPhone)
public void changedTextOnEditPhone() {
tlayoutEdtPhone.setError("");
}
如果你想了解ButterKnife,我写了一篇更详细的帖子,但它是以我的母语,即pt_br完成的。 http://blog.alura.com.br/aumentando-a-produtividade-com-butter-knife-no-android/
答案 4 :(得分:2)
class wizard(models.TransientModel):
_name = 'model.name'
department_id = fields.Many2one('hr.department')
employee_id = fields.Many2one('hr.employee')
date_from = fields.Date(string = 'Start Date', required = True)
date_to = fields.Date(string = 'End Date', required = True)
state = fields.Selection([('draft', 'Draft'), ('verify', 'Waiting'),('done', 'Done'),('cancel', 'Rejected'),])
def generate(self, cr, uid, ids, context=None):
return self.write(cr, uid, ids, {'state': 'draft'}, context=context)
答案 5 :(得分:1)
遗憾的是,没有内置机制可以实现此行为。我创建了Transform
类,可以帮助我:
ViewUtils
然后您可以轻松地在客户端代码中使用它:
public final class ViewUtils {
private ViewUtils() {}
public static void resetTextInputErrorsOnTextChanged(TextInputLayout... textInputLayouts) {
for (final TextInputLayout inputLayout : textInputLayouts) {
EditText editText = inputLayout.getEditText();
if(editText != null) {
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
}
@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
}
@Override
public void afterTextChanged(final Editable s) {
if(inputLayout.getError() != null) inputLayout.setError(null);
}
});
}
}
}
}
答案 6 :(得分:0)
如果要从TextInputLayout中删除错误显示,请使用: -
YourtextInputLayout.setErrorEnabled(false);
否则 如果要从edittextfield中删除错误显示,请使用: -
edittext_id.setError(null);
我建议在Android中使用 Textwatcher 功能,以便更有效地进行验证处理..
例如:
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 (!validatefName()) {
return;
}
}
});
答案 7 :(得分:0)
这就是我使用扩展TextInputLayout
的自定义类完成此操作的方式。像其他答案一样使用TextWatcher
,但是在xml中使用它之后,无需手动执行任何操作。它为您照顾一切。
public class CustomTextInputLayout extends TextInputLayout {
public CustomTextInputLayout(Context context) {
super(context);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
initRemoveErrorWatcher();
}
private void initRemoveErrorWatcher() {
EditText editText = getEditText();
if (editText != null) {
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) {
setError(null);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
}
要使用,只需将xml中的TextInputLayout
替换为:
<com.example.customViews.CustomTextInputLayout>...
答案 8 :(得分:0)
使用 KTX:
textInputLayout.editText?.doOnTextChanged { text, start, count, after ->
if (text?.any(invalidCharacters.toCharArray()::contains) == true) {
textInputLayout.error = "Invalid character entered: ${invalidCharacters}"
} else {
textInputLayout.error = null
}
}
答案 9 :(得分:-1)
在这里使用textwatcher链接http://www.learn2crack.com/2014/02/android-textwatcher-example.html
答案 10 :(得分:-1)
使用if else条件,如果condition为true,则设置TextInoutLayoutObj.setError(null);如果它的错误设置你的错误