Android 常用且比较好看的EditText(二)—— TextInputEditText

Android EditText

之前讲了一个带动画的EditText
这一篇来讲一下
对于我个人也比较常用的一个EditText —— Material Design 的 TextInputEditText
网上也有很多文章关于这个TextInputEditText
很多属性也都归纳过
这里我就简单讲述一下 我使用的
仅为个人自己所学习和使用的

首先 TextInputEditText 要和TextInputLayout 搭配使用
TextInputLayout 就等同于一个线性布局,TextInputEditText的限制属性也可以在TextInputLayout 中描述声明

样例

简单放一下样例
在这里插入图片描述
在这里插入图片描述
样例中,字数超过限制后会相应的报错,如果用于密码还可以设置显示和隐藏密码
之前也写过文章关于这个隐藏密码的问题
Android TextInputEditText初始隐藏密码

图一xml

下面分别给出两张图的两段代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/cloud"
    tools:context=".ui.LoginActivity">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="320dp"
        android:layout_height="320dp"
        android:layout_centerInParent="true"
        android:background="@drawable/shape_mine_top1"
        android:elevation="5dp"
        android:orientation="vertical"
        android:paddingTop="10dp">

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/account"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableStart="@drawable/identification"
                android:drawableLeft="@drawable/identification"
                android:hint="请输入账号"
                android:imeOptions="actionNext"
                android:inputType="text" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            app:hintAnimationEnabled="true"
            app:hintEnabled="true"
            app:passwordToggleEnabled="true"
            app:passwordToggleTint="@color/toggle">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableStart="@drawable/password"
                android:drawableLeft="@drawable/password"
                android:hint="请输入密码"
                android:imeOptions="actionDone"
                android:inputType="number" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.button.MaterialButton
            android:id="@+id/submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="25dp"
            android:backgroundTint="#FFFFFF"
            android:paddingLeft="25dp"
            android:paddingRight="25dp"
            android:text="确定"
            android:textColor="#CC9933"
            app:cornerRadius="15sp"
            app:icon="@drawable/right"
            app:iconSize="20dp"
            app:iconTint="#CC9933"
            app:strokeColor="#7FFFD4"
            app:strokeWidth="2dp" />


        <com.google.android.material.textview.MaterialTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="注册账号"
            android:textColor="#808080"
            android:textSize="11sp"/>


    </androidx.appcompat.widget.LinearLayoutCompat>

</RelativeLayout>

图二xml

<?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=".button_list.TextInputEdit">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/text_edit"
        app:counterEnabled="true"
        app:counterMaxLength="8"
        app:counterOverflowTextAppearance="@style/counterOverFlow"
        app:counterTextAppearance="@style/CounterStyle"
        app:hintAnimationEnabled="true"
        app:hintEnabled="true">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/man"
            android:hint="请输入相应内容"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:paddingLeft="10dp"
            android:textColorHint="#fff" />
    </com.google.android.material.textfield.TextInputLayout>


    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="10dp"
        android:hint="请输入相应内容"
        android:background="@drawable/second_text_edit"
        app:counterEnabled="true"
        app:counterMaxLength="6"
        app:counterOverflowTextAppearance="@style/counterOverFlow"
        app:hintAnimationEnabled="true"
        app:hintEnabled="true"
        app:passwordToggleEnabled="true"
        app:passwordToggleTint="@color/toggle">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionNext"
            android:inputType="number"/>


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

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:hint="请输入相应内容"
        app:counterEnabled="true"
        app:counterMaxLength="6"
        app:hintAnimationEnabled="true"
        app:hintEnabled="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/textInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionDone"
            android:inputType="number"/>

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

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:hint="请输入相应内容"
        app:counterEnabled="true"
        app:counterMaxLength="6"
        app:hintAnimationEnabled="true"
        app:hintEnabled="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/textInputEditText_second"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionDone"
            android:inputType="number"/>

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



</LinearLayout>

这里仅仅提及我上图所使用的属性
且属性说明比较口语化

涉及字体样式的,图中使用colors和style的可以定义颜色

属性属性说明
android:drawableLeft在输入框左边添加图片
app:hintAnimationEnabledhint的动画是否显示,默认显示,默认值为true
app:hintEnabledhint是否使用,默认使用,默认值为true
app:passwordToggleEnabled是否使用密码隐藏和显示,需要设置EditText inputType
app:passwordToggleTint密码显示和隐藏图片颜色
app:counterEnabled是否使用计数器
app:counterMaxLength计数器的最大值,需要使用计数器
app:counterOverflowTextAppearance超过计数器最大值时字体样式
app:counterTextAppearance计数器的字体的样式

可以看到图二中,第三第四个是有错误的提示的,这个是自定义的样式
接下来通过活动来编写

错误提示样式一

activity

	private TextInputLayout textInputLayout;
	private TextInputEditText textInputEditText;        //EditText错误信息

	textInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout);
	textInputEditText = (TextInputEditText) findViewById(R.id.textInputEditText);   //EditText错误信息
 
	makerEditText();

    //自定义错误信息
    public void makerEditText() {
        textInputEditText.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (textInputEditText.getText().length() > textInputLayout.getCounterMaxLength()) {
                    textInputEditText.setError("已经超过限定字数了!! 需要重新输入");
                }
            }
        });
    }

这一个自定义的错误信息是设置在EditText(TextInputEditText)的
通过监听TextInputEditText,判断输入的是否大于规定的计数器的最大值,超过则显示错误信息,setError

错误提示样式二

activity

	private TextInputLayout newTextInputLayout;
    private TextInputEditText newTextInputEditText;    //Layout错误信息

	newTextInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout_second);
    newTextInputEditText = (TextInputEditText) findViewById(R.id.textInputEditText_second);  //Layout错误信息	
    
   	makerLayout();

    private void makerLayout() {
        newTextInputEditText.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (newTextInputEditText.getText().length() > newTextInputLayout.getCounterMaxLength()) {
                    newTextInputLayout.setError("超过规定字数!请更改内容");
                } else {
                    newTextInputLayout.setErrorEnabled(false);
                }
            }
        });
    }

这个错误信息是设置在TextInputLayout的,所以图中的第三个输入框错误信息在TextInputEditText上,第四个在TextInputLayout上
也是通过监听TextInputEditText,判断是否字数超过计数器最大值


错误信息样式关键点

ps:细心的会发现都是显示错误信息,而样式二的代码有else的情况,这是为什么?

原因是:因为错误信息设置在TextInputLayout上,如果不写else的内容,
第一次的输入超过计数器最大值而显示错误信息后,此时退格删除输入的信息使输入的长度小于计数器最大值但是却会发现错误信息不会消失

所以需要设置一个else,当退格删除后,小于计数器最大值,保证错误信息消失、不显示 newTextInputLayout.setErrorEnabled(false);


总结

这个就是自己经常使用的EditText的第二部分,
Material Design的内容还是比较好看的
自己也比较喜欢用这个,平时出现的问题也努力解决以达到想要的效果

以后还会继续发表一些自己比较喜欢使用的控件

学习Android的路还很长,大家共同努力❤

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Android的Widget是指可以被放置在桌面或者其他应用中的小部件,比如常见的时钟、天气、日历等等。在Android中,Widget的布局可以使用XML文件来进行定义,与普通的布局定义类似,可以使用各种属性来设置Widget的样式和行为。 在Widget的XML布局文件中,可以使用View类中定义的许多属性来控制Widget的样式和行为,比如: 1. android:id:设置Widget的ID,可以在代码中通过findViewById()方法来获取对应的View对象。 2. android:layout_width、android:layout_height:设置Widget的宽度和高度,可以使用具体数值或者match_parent、wrap_content等特殊值。 3. android:layout_gravity:设置Widget在父布局中的对齐方式,比如center、left、right等等。 4. android:padding、android:paddingLeft、android:paddingRight等:设置Widget的内边距,用于控制Widget内部内容的显示位置。 5. android:background:设置Widget的背景颜色或者背景图片。 6. android:clickable、android:longClickable:设置Widget是否可以被点击或者长按。 7. android:focusable、android:focusableInTouchMode:设置Widget是否可以获取焦点,用于控制Widget是否可以响应键盘事件等。 8. android:visibility:设置Widget是否可见,可以使用值为visible、invisible、gone。 除了以上列出的属性之外,还有许多其他的属性可以用于控制Widget的样式和行为,具体可以查看Android官方文档。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

iㅤ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值