TextInputLayout

这个控件的两个优点:

  1. 更加友好地显示提示
  2. 更友好地显示错误信息

更加友好地显示提示:

<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:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lovely_solory.super_william.mynotebook.MainActivity">
    <android.support.design.widget.TextInputLayout
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="username"
            android:maxLength="25"
            android:maxLines="1" />
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/username">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:hint="password"
            android:maxLength="25"
            android:maxLines="1" />
    </android.support.design.widget.TextInputLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/password"
        android:layout_marginTop="20dp"
        android:text="login" />
</RelativeLayout>


更加友好地显示错误信息:
这部分需要代码来控制:

public class MainActivity extends AppCompatActivity {
    TextInputLayout edit_username, edit_password;
    Button btn;

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

        edit_username = findViewById(R.id.username);
        edit_password = findViewById(R.id.password);
        btn = findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = edit_username.getEditText().getText().toString();
                String password = edit_password.getEditText().getText().toString();

                if (username.isEmpty()) {
                    edit_username.setErrorEnabled(true);
                    edit_username.setError("请输入正确的用户名格式");
                }
                if (password.isEmpty()) {
                    edit_password.setErrorEnabled(true);
                    edit_password.setError("请输入正确的密码格式");
                }
                if (!username.isEmpty()&&!password.isEmpty())
                {
                    Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                }

            }
        });


    }
}

效果:

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 如果你已经打算阅读 rails 源代码了,你一定已经安装好 ruby 和 rails 了,可能和我一样你也是用 r...
    z_k阅读 207评论 0 1
  • 粗体与斜体这里是粗体这里是斜体**这里是粗体***这里是斜体* 标题 一级标题 二级标题 三级标题 #一级标题##...
    绝决阅读 255评论 0 0
  • 读书随笔:有的学生是因为教师缺乏课堂管理的技巧,没有建立新的课堂学生行为规范,如有的教师发现学生上课不专心,明知学...
    刘春晖11阅读 200评论 0 0