安卓TextinputLayout使用方法

目录

导入依赖

TextinputLayout

TextinputLayout标签基本属性

加入事件(java篇)


导入依赖

使用Design下的TextinputLayout前导入依赖步骤

在这个页面中直接找到design这个依赖,点击后再点击这两个窗口上的OK按钮,等待编译完成就完成导入了!

(可能版本不一样,导入即可)


TextinputLayout

TextinputLayout在Design下,可以配合EditText或者EditText的子类使用,通常可以用来提示错误信息,或者显示提示字数限制等一些操作,还可以用来做密码框密码显示!这些使用,只需要在属于TextinputLayout的标签内加入相应的属性设置对应的事件即可,先来看下最简单的效果

当在这个文本框输入信息的时候,提示文字会自动向上移动;

第一个和第二个文本框使用了TextinputLayout,第三个未普通的EditText,我设置了显示字数长度和密码框的显示,看下我的布局文件

<?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"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    tools:context=".MainActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="输入用户名(TextInputLayout)"

        app:counterEnabled="true"
        app:counterMaxLength="6"
        app:errorEnabled="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.TextInputLayout>


    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入密码(TextInputLayout)"
        app:passwordToggleEnabled="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />

    </android.support.design.widget.TextInputLayout>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        
        android:hint="请输入手机号(普通的EditText)" />

</LinearLayout>

 

TextinputLayout标签基本属性

属性详细介绍
 android:hint=" "提示输入内容
app:hintEnabled=“true”是否显示提示(hint),默认为true
app:hintAnimationEnabled="true"设置动画,是否有动画,默认为true
app:hintTextAppearance=" "提示文本的样式

app:errorEnabled="true"

错误提示是否为显示
app:errorTextAppearance=" "设置错误提示的样式

app:passwordToggleEnabled="true"

设置是否为可显示密码(上图右边的小眼睛)
app:passwordToggleDrawable=" "定义显示密码按钮的图标
app:passwordToggleTintMode=" "设置显示密码按钮的模式(screen、src_atop、multiply、src_in、src_over)
app:passwordToggleTint=" "为显示密码按钮设置颜色
app:counterEnabled=" "是否显示字数限制(如图第一个文本框右下角)
app:counterMaxLength=" "最大的输入长度

app:counterEnabled=" "

是否显示长度限制(true、false)
app:counterTextAppearance=" "长度提示的样式

加入事件(java篇)

实现目标:点击注册按钮不符合要求后输入框下提示字数不符合要求。(在这里只对用户名做出了限制,举一反三即可)

》》》AndroidStudio快速实例化-插件安装与使用《《《点击进入

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private TextInputLayout username;
    private TextInputLayout pasword;
    private EditText phone_number;
    private Button register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        username = (TextInputLayout) findViewById(R.id.username);
        pasword = (TextInputLayout) findViewById(R.id.pasword);
        phone_number = (EditText) findViewById(R.id.phone_number);
        register = (Button) findViewById(R.id.register);

        register.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register:
                submit();
                //判断字数,设置事件
                username.setErrorEnabled(true);//提示信息设置为可见
                username.setError("字数不符合要求");

                if (username.getEditText().getText().length() == 0 || username.getEditText().getText().length() > 6) {
                    username.setError("字数不符合要求");
                } else {
                    username.setErrorEnabled(false);//提示信息设置为不可见
                }
                break;
        }
    }

    //判断手机号输入是否为空
    private void submit() {
        // validate
        String number = phone_number.getText().toString().trim();
        if (TextUtils.isEmpty(number)) {
            Toast.makeText(this, "请输入手机号(普通的EditText)", Toast.LENGTH_SHORT).show();
            return;
        }
        // TODO validate success, do something
    }
}

再来一遍布局文件

<?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"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    tools:context=".MainActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="输入用户名(TextInputLayout)"
        app:counterEnabled="true"
        app:counterMaxLength="6"
        app:errorEnabled="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.TextInputLayout>


    <android.support.design.widget.TextInputLayout
        android:id="@+id/pasword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入密码(TextInputLayout)"
        app:passwordToggleEnabled="true"
        app:passwordToggleTint="@color/design_default_color_primary_dark">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />

    </android.support.design.widget.TextInputLayout>

    <EditText
        android:id="@+id/phone_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"

        android:hint="请输入手机号(普通的EditText)" />

    <Button
        android:id="@+id/register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="注册" />

</LinearLayout>

效果演示:

 

Android TextInputLayout是一个支持Material Design风格的输入框容器,它可以用来包装任何EditText或EditText的子类,提供了一些功能,如错误提示、计数器、密码可见性切换等。 TextInputLayout的主要功能有: 1.错误提示:当用户输入无效数据时,可以显示错误消息。 2.计数器:可以显示已输入字符的数量和最大字符数量。 3.密码可见性切换:可以在明文和密码模式之间切换。 4.动画效果:包含了一些动画效果,如标签浮动和错误消息上移等。 使用TextInputLayout,需要在XML中将EditText包装在TextInputLayout中,并在TextInputLayout中设置相关属性。例如: ``` <com.google.android.material.textfield.TextInputLayout android:id="@+id/textInputLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your name"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout> ``` 在代码中,可以使用以下方法来设置错误消息、计数器等: ``` // 设置错误消息 textInputLayout.setError("Invalid input"); // 显示计数器 textInputLayout.setCounterEnabled(true); textInputLayout.setCounterMaxLength(50); // 密码可见性切换 textInputLayout.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE); ``` 总之,TextInputLayout是一个非常有用的控件,可以提高Android应用程序的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

第三女神程忆难

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

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

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

打赏作者

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

抵扣说明:

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

余额充值