0
点赞
收藏
分享

微信扫一扫

[Android Material Design]组件08 - TextInputLayout

斗米 2022-03-30 阅读 200

文章目录

效果图

TextInputLayout

关键代码

xml布局文件代码如下

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- app:counterEnabled 是否显示计数器 -->
    <!-- app:counterMaxLength 设置计数器的最大值 -->
    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/usr_til"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="50dp"
        app:counterEnabled="true"
        app:counterMaxLength="10"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <EditText
            android:id="@+id/usr_edt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="用户名" />
    </com.google.android.material.textfield.TextInputLayout>

    <!-- app:passwordToggleEnabled 是否显示密码开关图片 -->
    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/pwd_til"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="50dp"
        app:passwordToggleEnabled="true"
        app:layout_constraintTop_toBottomOf="@id/usr_til"
        app:layout_constraintLeft_toLeftOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/pwd_tie"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="密码"
            android:inputType="textPassword" />
    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

TextInputLayout控件对EditText进行了增强,当编辑框输入字符时,hint提示文字会显示在编辑框的上方,而且有一个滑上去的动画,给人丝滑的感觉。要正确使用该控件,需要注意以下两点:

  1. 编辑框所在的Activity需要继承自AppCompatActivity
  2. TextInputLayout作为容器只能包含一个EditText或者TextInputEditText控件;

TextInputEditTextEditText的区别
横屏时,TextInputLayoutEditText组合使用时,编辑框内的提示文字不见了,而TextInputLayoutTextInputEditText组合使用就能解决这个BUG。
竖屏时,TextInputEditTextEditText这两个控件没有区别。

源码地址

https://github.com/yurensan/MaterialDesignDemo

举报

相关推荐

0 条评论