Android TextInputLayout 实现动画登录界面

普通的登录界面图片:
在这里插入图片描述
EditText 的hint带动画效果,如图:
在这里插入图片描述
在这里插入图片描述
TextInputLayout 控件是一个容器,它跟 ScrollView 一样只接受一个子元素,并且这个子元素是 EditText。

activity_main.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:id="@+id/activity_text_input_layout"
    tools:context=".MainActivity">
    
    <android.support.design.widget.TextInputLayout
        android:id="@+id/tl_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

    <EditText
        android:id="@+id/edt_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:hint="用户名"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:maxLength="25"
        android:maxLines="1"/>

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

    <android.support.design.widget.TextInputLayout
        android:id="@+id/tl_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tl_username"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="20dp">

    <EditText
        android:id="@+id/edt_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edt_username"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:hint="密码"
        android:maxLength="25"
        android:maxLines="1"/>

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

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tl_password"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="20dp"
        android:text="登录"/>
    
</RelativeLayout>

显示错误信息,用来提示用户:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
MainActivity.java:

public class MainActivity extends AppCompatActivity {
    private TextInputLayout tl_username,tl_password;
    private Matcher matcher;
    private Button button;

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

        tl_username = findViewById(R.id.tl_username);
        tl_password = findViewById(R.id.tl_password);
        button = findViewById(R.id.btn_login);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        
    }

    private void login() {

        String username = tl_username.getEditText().getText().toString();       //  用户名
        String password = tl_password.getEditText().getText().toString();       //  密码
        if (!validateUsername(username)){
            tl_username.setErrorEnabled(true);
            tl_username.setError("账号不能超过3位");
        }else if (!validatePassword(password)){
            tl_password.setErrorEnabled(true);
            tl_password.setError("密码不能少于3位");
        }else{
            tl_username.setErrorEnabled(false);
            tl_password.setErrorEnabled(false);
            Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
        }

    }

    private boolean validatePassword(String password){
        return password.length() > 3;       //  字符长度不能小于3位
    }

    private boolean validateUsername(String username){
        return username.length() < 3;        //  字符长度不能大于3位
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王睿丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值