我需要一个看起来像这样 onError 的 EditText:
调用 onError 如下所示:
错误消息将显示在顶部,浮动标签提示将更改为错误消息。我尝试了一些方法。但它不会按设计来。
我需要一个看起来像这样 onError 的 EditText:
调用 onError 如下所示:
错误消息将显示在顶部,浮动标签提示将更改为错误消息。我尝试了一些方法。但它不会按设计来。
请试试这个兄弟
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exa);
inputLayoutEmail = (TextInputLayout) findViewById(R.id.input_layout_email);
inputLayoutPassword = (TextInputLayout) findViewById(R.id.input_layout_password);
inputEmail = (EditText) findViewById(R.id.input_email);
inputPassword = (EditText) findViewById(R.id.input_password);
btnSignUp = (Button) findViewById(R.id.btn_signup);
inputEmail.addTextChangedListener(new MyTextWatcher(inputEmail));
inputPassword.addTextChangedListener(new MyTextWatcher(inputPassword));
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
submitForm();
}
});
}
private void submitForm() {
if (!validateEmail()) {
return;
}
if (!validatePassword()) {
return;
}
Toast.makeText(getApplicationContext(), "Thank You!", Toast.LENGTH_SHORT).show();
}
private boolean validateEmail() {
String email = inputEmail.getText().toString().trim();
if (email.isEmpty() || !isValidEmail(email)) {
inputLayoutEmail.setHint(getString(R.string.err_msg_email));
requestFocus(inputEmail);
return false;
} else {
inputLayoutEmail.setErrorEnabled(false);
inputLayoutEmail.setHint("Email");
}
return true;
}
private boolean validatePassword() {
if (inputPassword.getText().toString().trim().isEmpty()) {
inputLayoutPassword.setHint(getString(R.string.err_msg_password));
requestFocus(inputPassword);
return false;
} else {
inputLayoutPassword.setErrorEnabled(false);
inputLayoutPassword.setHint("Password");
}
return true;
}
private static boolean isValidEmail(String email) {
return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
public void afterTextChanged(Editable editable) {
switch (view.getId()) {
case R.id.input_email:
validateEmail();
break;
case R.id.input_password:
validatePassword();
break;
}
}
}
android.support.design.widget.TextInputLayout
android:theme="@style/EditTextHint"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
或使用此editText.setHintTextColor(getResources().getColor(R.color.red));
尝试使用红色错误
在您的 activity_name.xml 中:
<android.support.design.widget.TextInputLayout
android:id="@+id/fromLoc_eLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="0.5"
android:gravity="center"
app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout">
<EditText
android:id="@+id/fromLocTravel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
android:hint="From"
android:imeOptions="flagNoExtractUi"
android:inputType="textCapSentences|textPersonName"
android:maxLength="50"
android:singleLine="true"
android:textColor="#000"
android:textSize="14sp"
android:theme="@style/EditTextTheme" />
</android.support.design.widget.TextInputLayout>
在你的 NameActivity.class 中:
TextInputLayout fromLoc_eLayout = (TextInputLayout) findViewById(R.id.fromLoc_eLayout);
EditText fromLocTravel = (EditText) findViewById(R.id.fromLocTravel);
textChangedListener
如果你想像下面这样显示Error messages
,你也可以实现hint
如下:
fromLocTravel.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
fromLoc_eLayout.setHint("your error message");
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});