Kis*_*ava 17 android material-design android-textinputlayout
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);
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
Gan*_*mar 17
为了进一步说明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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
做
mTextInputLayout.setError(null);
Run Code Online (Sandbox Code Playgroud)
清除错误消息.
一个好的做法可以是检查这样的错误的方法:
@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
}
}
Run Code Online (Sandbox Code Playgroud)
这样,它会在设置新错误消息之前刷新错误消息.
我使用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("");
}
Run Code Online (Sandbox Code Playgroud)
如果你想了解ButterKnife,我写了一篇更详细的文章,但它是用我的母语完成的,即pt_br.http://blog.alura.com.br/aumentando-a-produtividade-com-butter-knife-no-android/
| 归档时间: |
|
| 查看次数: |
40733 次 |
| 最近记录: |