1.布局
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phoneInput"
app:layout_constraintTop_toBottomOf="@id/startSnackBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_Phone"
android:hint="请输入手机号"/>
</com.google.android.material.textfield.TextInputLayout>
2.设置错误信息
TextInputLayout passwordInput = findViewById(R.id.passwordInput);
Button button = findViewById(R.id.login);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!passwordInput.getEditText().getText().toString().equals("qwe")) {
passwordInput.setErrorEnabled(true);
passwordInput.setError("密码错误 请重新输入");
}
}
});