TextInputLayout使用以及EditText自己实现监听

来源:互联网 发布:unix 网络编程 源代码 编辑:程序博客网 时间:2024/06/05 03:54
  • TextInputLayout介绍:

首先呢,TextInputLayout是什么?

TextInputLayout是一个能够把EditText包裹在当中的一个布局,当输入文字时,它可以把Hint文字飘到EditText的上方,它里面只能包裹一个布局,且只能是EditText,我们可以对其进行判断,当发生错误时,提示错误信息,在文本的下方。

实战:

1.添加依赖dependencies {  compile 'com.android.support:appcompat-v7:22.2.0'  compile 'com.android.support:design:22.2.0'}

然后呢

2.xml中声明一个TextInputLayout(里面套一个EditText)<android.support.design.widget.TextInputLayout    android:id="@+id/input_layout"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content" /></android.support.design.widget.TextInputLayout>

下一步:

3.实现监听和一些逻辑判断inputLayout.setHint("请输入姓名");    EditText text=inputLayout.getEditText();    inputLayout.setErrorEnabled(true);    text.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) {            if(s.length()>6){                inputLayout.setError("姓名长度不能大于6");            }else{                inputLayout.setError(null);            }        }        @Override        public void afterTextChanged(Editable s) {        }

下一步:

4.注意上面几个方法的作用:通过TextInputLayout.getEditText()获取输入框显示错误信息;    textInputLayout.setErrorEnable(true);    textInputLayout.setError(msg);隐藏错误信息:    textInputLayout.setErrorEnable(false);    textInputLayout.setError(null);

上面是对TextInputLayout的一些理解。

只用EditText实现原理是一样的————————————-
-
1.我们需要去实现TextWatcher这个接口,里面会有三个方法让我们操作

    1.文本改变前:beforeTextChanged    2.文本改变:onTextChanged    3.文本变化之后 afterTexhChanged

2.里面的一个方法设置

1. 设置光标到指定位置EditText et = (EditText) findViewById(R.id.etTest);et.setSelection(2);PS:当内容过多时,可通过设置光标位置来让该位置的内容显示在屏幕上。2. 隐藏光标 EditText et = (EditText) findViewById(R.id.etTest);//设置光标不显示,但不能设置光标颜色et.setCursorVisible(false);

3 android EditText获取光标位置并插入字符删除字符

1.获取光标位置int index = editText.getSelectionStart();  2.在光标处插入字符int index = editText.getSelectionStart();  Editable editable = editText.getText();  editable.insert(index, "aaaa");  3.删除光标前字符int index = editText.getSelectionStart();  Editable editable = editText.getText();  editable.delete(index-1, index);  4.android:textCursorDrawable   这个属性是用来控制光标颜色的"@null"   是作用是让光标颜色和text color一样android:textCursorDrawable="@null"

4—顺便介绍一下xml的inputtype的值。

android:inputType=”none”android:inputType=”text”android:inputType=”textCapCharacters” 字母大写android:inputType=”textCapWords” 首字母大写android:inputType=”textCapSentences” 仅第一个字母大写android:inputType=”textAutoCorrect” 自动完成android:inputType=”textAutoComplete” 自动完成android:inputType=”textMultiLine” 多行输入android:inputType=”textImeMultiLine” 输入法多行(如果支持)android:inputType=”textNoSuggestions” 不提示android:inputType=”textUri” 网址android:inputType=”textEmailAddress” 电子邮件地址android:inputType=”textEmailSubject” 邮件主题android:inputType=”textShortMessage” 短讯android:inputType=”textLongMessage” 长信息android:inputType=”textPersonName” 人名android:inputType=”textPostalAddress” 地址android:inputType=”textPassword” 密码android:inputType=”textVisiblePassword” 可见密码android:inputType=”textWebEditText” 作为网页表单的文本android:inputType=”textFilter” 文本筛选过滤android:inputType=”textPhonetic” 拼音输入//数值类型android:inputType=”number” 数字android:inputType=”numberSigned” 带符号数字格式android:inputType=”numberDecimal” 带小数点的浮点格式android:inputType=”phone” 拨号键盘android:inputType=”datetime” 时间日期android:inputType=”date” 日期键盘android:inputType=”time” 时间键盘

5.在代码里的应用

    EditText testEditText = (EditText) findViewById(R.id.edittext);      int inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL;      testEditText.setInputType(inputType);  

那我贴图看一下源码中InputType属性类吧。如下图,

这里写图片描述

6.关于EdiitText的其他属性

android:layout_gravity="center_vertical" 设置控件显示的位置:默认top,这里居中显示,还有bottom android:hint="请输入数字!"设置显示在空间上的提示信息android:numeric="integer" 设置只能输入整数,如果是小数则是:decimal android:singleLine="true" 设置单行输入,一旦设置为true,则文字不会自动换行。android:password="true" 设置只能输入密码android:textColor = "#ff8c00" 字体颜色android:textStyle="bold" 字体,bold, italic, bolditalic android:textSize="20dip" 大小android:capitalize = "characters" 以大写字母写android:textAlign="center" EditText没有这个属性,但TextView有 android:textColorHighlight="#cccccc" 被选中文字的底色,默认为蓝色android:textColorHint="#ffff00" 设置提示信息文字的颜色,默认为灰色android:textScaleX="1.5" 控制字与字之间的间距android:typeface="monospace" 字型,normal, sans, serif, monospaceandroid:background="@null" 空间背景,这里没有,指透明android:layout_weight="1" 权重,控制控件之间的地位,在控制控件显示的大小时蛮有用的。 通过EditText的layout xml文件中的相关属性来实现:1. 密码框属性 android:password="true" 这条可以让EditText显示的内容自动为星号,输入时内容会在1秒内变成*字样。2. 纯数字 android:numeric="true" 这条可以让输入法自动变为数字输入键盘,同时仅允许0-9的数字输入3. 仅允许 android:capitalize="cwj1987" 这样仅允许接受输入cwj1987,一般用于密码验证 下面是一些扩展的风格属性android:editable="false" 设置EditText不可编辑android:singleLine="true" 强制输入的内容在单行android:ellipsize="end" 自动隐藏尾部溢出数据,一般用于文字内容过长一行无法全部显示时

7.实现EditText为空时的抖动效果

如果开启抖动效果的话et.setAnimation(am);这个需要用到父类的requestLayout()方法ViewParent parent = et.parent();((ViewGroup)parent).requestLayout();Animation am = AnimationUtils.loadAnimation(this,R.anim.shake);公司中一般用et.startAnimation(am);在代码中直接实现动画效果(这样最方便)Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);translateAnimation.setInterpolator(new CycleInterpolator(7));translateAnimation.setDuration(1000);et_01.startAnimation(translateAnimation);在xml中实现anim--><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:fromXDelta="0"    android:interpolator="@anim/cycle_7"    android:toXDelta="10" />anim-->cycle_7<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"    android:cycles="7" />
1 0
原创粉丝点击