TextInputLayout 与 TextInputEditText
1、基本介绍
public class TextInputLayout extends LinearLayout {
...
}
public class TextInputEditText extends AppCompatEditText {
...
}
2、演示
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tiy_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tiet_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tiy_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
app:counterEnabled="true"
app:counterMaxLength="20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tiet_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tiy_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
app:counterEnabled="true"
app:counterMaxLength="20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tiet_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
TextInputLayout tiyUsername = findViewById(R.id.tiy_username);
TextInputEditText tietUsername = findViewById(R.id.tiet_username);
Button btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener(v -> {
String username = tietUsername.getText().toString().trim();
if (username.isEmpty()) {
tiyUsername.setError("请输入用户名");
return;
}
if (username.length() < 6) {
tiyUsername.setError("用户名长度不能小于 6 位");
return;
}
tiyUsername.setError(null);
});
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tiy_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
app:counterEnabled="true"
app:counterMaxLength="20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tiet_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
TextInputLayout tiyUsername = findViewById(R.id.tiy_username);
TextInputEditText tietUsername = (TextInputEditText) tiyUsername.getEditText();
tietUsername.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// 文本发生改变前的处理
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// 文本发生改变时的处理
}
@Override
public void afterTextChanged(Editable editable) {
// 文本发生改变后的处理
String username = editable.toString().trim();
if (username.isEmpty()) {
tiyUsername.setError("请输入用户名");
return;
}
if (username.length() < 6) {
tiyUsername.setError("用户名长度不能小于 6 位");
return;
}
if (username.length() > 20) {
tiyUsername.setError("用户名长度不能大于 20 位");
return;
}
tiyUsername.setError(null);
}
});
AppCompatEditText
1、基本介绍
- AppCompatEditText 用于创建兼容旧版本的 EditText,它继承自 EditText,并提供了一些额外的功能和改进
public class AppCompatEditText extends EditText implements TintableBackgroundView,
OnReceiveContentViewBehavior, EmojiCompatConfigurationView, TintableCompoundDrawablesView {
...
}
2、演示
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/acet_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword" />
网硕互联帮助中心




评论前必须登录!
注册