嗨,我正在使用 TextInputLayout 和 TextInputEditText 像这样
<android.support.design.widget.TextInputLayout
android:id="@+id/userName_til"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/error_appearance">
<android.support.design.widget.TextInputEditText
android:id="@+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username"
android:inputType="textEmailAddress"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
风格是
<style name="error_appearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@android:color/holo_red_light</item>
<item name="android:textSize">12sp</item>
</style>
Java 代码是
tilUserName = (TextInputLayout) findViewById(R.id.userName_til);
etUserName = (TextInputEditText) findViewById(R.id.etUserName);
etUserName.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) {
Timber.d("OnTextChanged Called");
if (s.length() < 1) {
tilUserName.setErrorEnabled(true);
tilUserName.setError(getString(R.string.please_enter_email_id));
} else if (!AppUtils.isValidEmail(s)) {
tilUserName.setErrorEnabled(true);
tilUserName.setError(getString(R.string.please_enter_valid_email_id));
} else {
tilUserName.setError(null);
tilUserName.setErrorEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
我在用...
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
在方向更改时自动显示 EditText 中的错误提示。
我该如何解决这个问题?