美文网首页
安卓6.0新控件TextInputLayout

安卓6.0新控件TextInputLayout

作者: 最黑暗的自己 | 来源:发表于2018-03-23 09:20 被阅读11次

TextInputLayout配合EditText使用

 <android.support.design.widget.TextInputLayout
            android:id="@+id/textInputLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:maxLength="10"//edittext字数限制
                android:id="@+id/account"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="账号" />
        </android.support.design.widget.TextInputLayout>
 textInputLayout.getEditText().addTextChangedListener(new MinWatcher());//设置监听器
        textInputLayout.setCounterEnabled(true);//开启字数统计
        textInputLayout.setCounterMaxLength(10);//字数统计最高值

  class MinWatcher implements 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) {  //edittext Change之后
            if (textInputLayout.getEditText().getText().toString().length()<=6){
                textInputLayout.setErrorEnabled(false);
                textInputLayout.setError("");
            }
            else {
                textInputLayout.setErrorEnabled(true);
                textInputLayout.setError("错误,错误!!!!!");
            }
        }
    }

  button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (textInputLayout.getEditText().getText().toString().isEmpty()){
                    textInputLayout.setErrorEnabled(true);
                    textInputLayout.setError("输入为空,重新输入!!!!!");
                }
                else {
                    Toast.makeText(getApplicationContext(),"登陆成功",Toast.LENGTH_SHORT).show();
                }
            }
        });

相关文章

网友评论

      本文标题:安卓6.0新控件TextInputLayout

      本文链接:https://www.haomeiwen.com/subject/fretcftx.html