云计算百科
云计算领域专业知识百科平台

Android 控件 - TextInputLayout 与 TextInputEditText、AppCompatEditText

TextInputLayout 与 TextInputEditText

1、基本介绍
  • TextInputLayout 是 Material Design 风格的文本输入容器,提供提供浮动标签、错误提示、字符计数等功能,父类是 LinearLayout
  • public class TextInputLayout extends LinearLayout {

    ...

    }

  • TextInputEditText 是专门用于在 TextInputLayout 内使用的 EditText
  • 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" />

    赞(0)
    未经允许不得转载:网硕互联帮助中心 » Android 控件 - TextInputLayout 与 TextInputEditText、AppCompatEditText
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!