Material Design 入门(一)——TextInputLayout和TextInputEditText
来源:互联网 发布:淘宝店主工具 编辑:程序博客网 时间:2024/06/06 05:13
最近学习了Material Design中提供的一些控件,下面就他们的使用方法陈述给大家,希望能够帮助大家快速上手。
本例子使用在控件都在com.android.support:design包中,如果使用的是Android studio开发工具,在build.gradle 中直接添加
compile 'com.android.support:design:24.1.1'即可。下面就开始Material Design的学习之旅吧。
本节为大家讲解的是TextInputLayout和TextInputEditText
1、TextInputLayout
java.lang.Object ↳android.view.View ↳android.view.ViewGroup ↳android.widget.LinearLayout ↳android.support.design.widget.TextInputLayout
Layout which wraps an EditText (or descendant) to show a floating label when the hint is hidden due to the user
inputting text.
根据TextInputLayout的继承结构可以看出,它就是一种新的layout布局,并且在这个布局中包含了EditText或者其子类这个控件,这个
布局可以显示一个浮动的文字,用来展示EditText的提示文字hint和EditText输入错误时的错误提示文字。
setHint():设置EditText的提示文字
setErrorEnabled(boolean):设置是否显示编辑框的错误提示
setError(CharSequence):设置编辑框的错误提示文字
下面给出基本例子:
<android.support.design.widget.TextInputLayout android:id="@+id/textInputLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimaryDark" android:padding="10dp" android:visibility="gone"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector_edittext_bg" android:inputType="number" android:padding="5dp" android:textColor="?attr/colorAccent" android:textColorHint="@android:color/holo_red_light"/></android.support.design.widget.TextInputLayout>Java代码如下:textInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout); textInputLayout.setHint("请输入4位学号"); editText = (EditText) findViewById(R.id.editText); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if (charSequence.length() > 4) {textInputLayout.setError("学号输入错误"); textInputLayout.setErrorEnabled(true); } else { textInputLayout.setErrorEnabled(false); } } @Override public void afterTextChanged(Editable editable) { } }); 效果如图:2、TextInputEditTextTextInputEditText是EditText的子类,可以和EditText一样和TextInputLayout一起使用实现hint提示和错误提示,只是UI效果有所差别。
setError():设置出错提示信息
代码如下:
<android.support.design.widget.TextInputLayout android:id="@+id/textInputLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimaryDark" android:padding="10dp" android:visibility="visible">Java代码和上面差不多<android.support.design.widget.TextInputEditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector_edittext_bg" android:inputType="number" android:padding="5dp" android:textColor="?attr/colorAccent" android:textColorHint="@android:color/holo_red_light" android:visibility="gone"/></android.support.design.widget.TextInputLayout>textInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout); textInputLayout.setHint("请输入4位学号"); editText = (EditText) findViewById(R.id.editText); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if (charSequence.length() > 4) { editText.setError("学号输入错误");// textInputLayout.setError("学号输入错误");// textInputLayout.setErrorEnabled(true); } else {// textInputLayout.setErrorEnabled(false); } } @Override public void afterTextChanged(Editable editable) { } });效果如图:(红色框内的字体颜色还没有找到方法修改,各位可以研究下,有方法可以给我留言。)
0 2
- Material Design 入门(一)——TextInputLayout和TextInputEditText
- Material Design 入门(一)——TextInputLayout和TextInputEditText
- Andorid Material Design TextInputLayout和TextInputEditText
- Android Material Design之TextInputLayout、TextInputEditText解析
- Material Design整理(一)——TextInputLayout
- Android Design Support Library(一):FloatingActionButton、TextInputLayout、TextInputEditText简单用法
- Android Design Support Library(一):FloatingActionButton、TextInputLayout、TextInputEditText简单用法
- Android Design Support Library(一):FloatingActionButton、TextInputLayout、TextInputEditText简单用法
- TextInputLayout和TextInputEditText使用
- Android Design Support Library使用详解——TextInputLayout与TextInputEditText
- Android Design Support Library使用详解——TextInputLayout与TextInputEditText
- Android进阶——Material Design新控件之TextInputLayout
- Android开发——Material Design系列之TextInputLayout
- Android中Design库之TextInputLayout、TextInputEditText
- Material Design (二),TextInputLayout的使用
- Material Design之TextInputLayout
- Material Design -- TextInputLayout
- Material Design之TextInputLayout
- Android网络与数据存储03-在线请求天气API,并解析其中的json数据予以显示
- POJ 3657 Haybale Guessing 并查集+二分答案
- 简单匹配游戏的制作
- 重新认识Activity
- 自定义圆环进度
- Material Design 入门(一)——TextInputLayout和TextInputEditText
- 通过 JSP Model 深入学习 MVC
- 最靠谱的CDH5安装指南
- 在数据库中怎么判断哪个(些)字段是主码
- Vijos P1850 小朋友的数字(动态规划,最大子段和)
- 利用redis缓存热门数据,分页的一种思路
- 【剑指offer】面试题10: 二进制中1的个数
- linux下编译安装ACE6
- php CI框架下封装datatable插件处理类