【Android -- Material Design】TextInputLayout 和 TextInputEditText 的基本使用

前言

官网:TextInputLayout
01.png

引入 material 包:

implementation 'com.google.android.material:material:1.2.1'

属性

属性含义
counterEnabled字符计数是否可用
counterMaxLength计数最大的长度
counterOverflowTextAppearance计数超过最大长度时显示的文本样式
counterTextAppearance显示的计数的文本样式
errorEnabled显示错误信息是否可用
errorTextAppearance显示错误信息的文本样式
hint浮动标签
hintEnabled控制是否显示浮动标签
hintTextAppearance浮动标签的文本样式
passwordToggleDrawable密码可见切换图标
passwordToggleEnabled控制是否显示密码可见切换图标

使用

  1. 带浮动标签的文本框
    002.gif

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        app:counterOverflowTextAppearance="@style/TextOverCount"
        android:scrollbarAlwaysDrawHorizontalTrack="true"
        android:textColorHint="@color/black">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="用户名"
            android:background="@color/white"
            android:inputType="text"
            android:textColor="@color/black"/>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        app:counterOverflowTextAppearance="@style/TextOverCount"
        android:scrollbarAlwaysDrawHorizontalTrack="true"
        android:textColorHint="@color/black">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:hint="手机号码"
            android:inputType="number"
            android:textColor="@color/black"/>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/materialButton"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="登录"
        android:textAllCaps="false" />

</LinearLayout>

2. 带字符计数的文本框
002.jpg

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/text_input_layout_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:textColorHint="@color/black">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/text_input_user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="用户名"
            android:background="@color/white"
            android:inputType="text"
            android:textColor="@color/black"/>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        app:counterOverflowTextAppearance="@style/TextOverCount"
        android:scrollbarAlwaysDrawHorizontalTrack="true"
        android:textColorHint="@color/black">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:hint="手机号码"
            android:inputType="number"
            android:textColor="@color/black"/>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/materialButton"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="登录"
        android:textAllCaps="false" />

</LinearLayout>

在 Activity 中设置,计数的长度:

public class MainActivity extends BaseActivity {

    @BindView(R.id.text_input_layout_user)
    TextInputLayout mTextInputLayout;



    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        // 设置可以计数
        mTextInputLayout.setCounterEnabled(true);
        // 计数的最大值
        mTextInputLayout.setCounterMaxLength(10);
    }

}

3. 显示密码可见和隐藏的切换按钮
03.jpg

布局

<?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">


    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:counterEnabled="true"
        app:counterMaxLength="3"
        app:counterOverflowTextAppearance="@style/MyOverflowText"
        app:errorEnabled="true"
        app:errorTextAppearance="@style/MyErrorText"
        app:hintTextAppearance="@style/MyHintText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:background="@color/white"
            android:hint="用户名"
            android:paddingStart="0dp"
            android:textSize="18sp" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/til_name"
        app:passwordToggleEnabled="true"
        app:passwordToggleTint="@color/green_deep_color">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:hint="密码"
            android:inputType="textPassword"
            android:paddingStart="0dp" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btn_login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="8dp"
        android:text="登录"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/til_password" />

</androidx.constraintlayout.widget.ConstraintLayout>

在 Activity 中设置:

public class MainActivity extends BaseActivity {
    @BindView(R.id.til_name)
    TextInputLayout mLayoutName;

    @BindView(R.id.et_name)
    TextInputEditText mName;

    @BindView(R.id.btn_login)
    MaterialButton mLogin;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mLayoutName.setErrorEnabled(false);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = mName.getText().toString().trim();
                if (name.length() > mLayoutName.getCounterMaxLength()) {
                    mLayoutName.setError("输入内容超过上限");
                }
            }
        });

    }

}

style 风格

    <style name="MyHintText" parent="TextAppearance.AppCompat.Small">
        <item name="android:textColor">@color/light_grey_color</item>
    </style>

    <style name="MyEditText" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/light_grey_color</item>
        <item name="colorControlActivated">@color/black</item>
        <item name="android:textSize">18sp</item>
    </style>

    <style name="MyErrorText" parent="TextAppearance.AppCompat.Small">
        <item name="android:textColor">@color/red_deep</item>
    </style>

    <!--Overflow label text style-->
    <style name="MyOverflowText" parent="TextAppearance.AppCompat.Small">
        <item name="android:textColor">@color/orange</item>
    </style>
  • 3
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 17
    评论
### 回答1: Android开发中使用Kotlin语言开发Material Design项目可以带来很多好处。Kotlin是一种现代化的编程语言,它可以提高开发效率、减少代码量、提高代码可读性和可维护性。同时,Material Design是一种现代化的设计语言,它可以提高用户体验、提高应用的可用性和可访问性。因此,使用Kotlin开发Material Design项目可以使应用更加现代化、高效、易于维护和易于使用。 ### 回答2: 随着移动设备的迅速普及,Android操作系统已经成为全球最流行的移动操作系统之一。这使得Android应用程序的开发变得越来越受欢迎,许多开发人员也开始学习和掌握这个平台。随着时间的推移,开发人员也在不断寻找最好的解决方案来创建优秀的应用程序,其中kotlin和material design就是两种最受欢迎的选择。 Kotlin是一种高级编程语言,它是Java虚拟机的官方语言之一。Kotlin在Android开发中的流行程度日益增加,因为它具有许多特性,如可空类型、lambdas、扩展函数等,使得开发Android应用程序更加便捷和高效。Kotlin支持Java虚拟机,并非Android特定的开发语言,因此具有更广泛的用途,可以与其他语言无缝集成。同时,Kotlin还有很多实用特性,如可空类型、类型推断、lambda表达式等,能够在Android开发中大幅提高开发效率,在代码中减少了很多荣誉的代码和冗长表达式的麻烦。 Material Design是一种设计语言,由Google推出,旨在为移动和Web应用程序提供一致的极致体验。Material Design提供了一系列的设计指南、模式和组件,以便开发人员可以为他们的应用程序在不同平台和设备上提供一致的体验,从而使应用程序更加具有现代感和吸引力。 Material DesignAndroid开发者提供了一些标准的界面组件,如浮动操作按钮、抽屉式导航等,同时也支持进行自定义设计,为应用程序增加独特的特色。 综合考虑,使用Kotlin和Material Design组合开发Android应用程序可以有很多好处。Kotlin可以使代码更加简洁,同时使用Material Design的组件和元素可以使应用程序显得更加现代和美观。 这种组合还可以提高开发效率,减少代码中的bug,在Android平台上提供更好的用户体验,从而为应用程序的成功打下坚实的基础。总的来说,采用这种开发方式的应用程序将具有更高的可维护性和可扩展性,也将在市场上拥有更高的竞争力。 ### 回答3: 在当前的移动应用开发市场中,Android系统已经成为了主流之一,其开发工具也被越来越多的开发者采用。而Kotlin语言作为一种新兴的编程语言,因其简洁、安全、互通性和易用性等特点,在Android系统开发中越来越受到开发者的欢迎。 在这种情况下,开发Material Design项目需要用到的技术和工具也应该是非常有趣的。 Android系统采用Material Design作为其UI设计风格,提供了一套完备的UI组件库。要开发Material Design的应用程序,需要遵循Google的Material Design规范以及使用相关的Android开发API和工具。Kotlin语言提供了很多的便利,在使用Android开发API和工具的同时,还提供了方便的语法和Lambda表达式。 使用Kotlin开发Material Design项目还可以提高应用程序的安全性,避免因类型不安全、空指针等问题导致的错误。Kotlin还支持函数式编程,可以提供更好的编写UI代码的方法,帮助开发人员高效编写代码,提高开发效率。 Kotlin增加了一些与Java不同的特性,使得开发者能够更快、更方便地编写代码。例如,Kotlin具有空安全机制,可帮助开发者尽早发现和解决可能导致应用程序崩溃的问题,从而提高应用程序的质量。Kotlin还提供了lambda表达式、扩展功能和集合操作,更方便开发人员在项目中增加新特性,缩短应用开发周期。 虽然Kotlin开发Material Design项目相对于Java来说还是一个相对新的领域,但随着Kotlin使用率的逐渐增加,越来越多的开发者正在使用和探索Kotlin在Android开发中的应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin-Dev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值